用Java在各种框架下编译Scala项目

开发 后端
如何利用Java在各种框架下编译Scala项目?本文转载自一位外国开发人员的blog,是一篇利用Java语言在最流行的各种Java框架下调用Scala的项目的教程,从细微入手解决实际问题。

【51CTO编译】51CTO曾经在“与Scala的***次亲密接触 Java如影相随”为大家讲过Java于Scala。下面是一个教程文件是我在如今***或者说***的Java框架——SpringHibernate/JPA、Wicket、Maven和Intellij IDEA上充分利用Java语言调用Scala的项目,希望能帮助其他的Java程序员使用一个企业级的Scala项目。

在IDE上的设置

首先,下载的IntelliJ IDEA的9.0.x(社区版),因为目前它是***的Scala的IDE。然后选择File |设置|插件,选择可用的选项卡来安装Scala插件。

创建Maven项目

在IntelliJ IDEA中,选择文件|新建项目,然后选择Maven的模块创建一个Maven项目。然后修改如下所示的pom.xml。添加所有你需要的设置,建立Scala的汇编语言:

  1. <project xmlnsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
  2.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">           
  3.     <modelVersion>4.0.0</modelVersion>           
  4.     <groupId>com.foo</groupId>           
  5.     <artifactId>myapp</artifactId>           
  6.     <packaging>war</packaging>           
  7.     <version>1.0-SNAPSHOT</version>           
  8.     <name>myapp Java EE 6 Webapp</name>           
  9.     <url>http://maven.apache.org</url>           
  10.     <properties>           
  11.         <spring.version>3.0.3.RELEASE</spring.version>           
  12.      </properties>          
  13.            
  14.     <repositories>           
  15.         <repository>           
  16.             <id>java.net2</id>           
  17.             <name>Repository hosting the jee6 artifacts</name>           
  18.             <url>http://download.java.net/maven/2</url>           
  19.          </repository>          
  20.            
  21.         <repository>           
  22.             <id>scala-tools.org</id>           
  23.              <name>Scala-tools Maven2 Repository</name>          
  24.             <url>http://scala-tools.org/repo-releases</url>           
  25.            
  26.         </repository>           
  27.         <repository>           
  28.              <id>wpt-release</id>          
  29.             <url>http://wicketpagetest.sourceforge.net/m2-repo/releases</url>           
  30.            
  31.         </repository>           
  32.         <repository>           
  33.              <id>wpt-snapshot</id>          
  34.             <url>http://wicketpagetest.sourceforge.net/m2-repo/snapshots</url>           
  35.            
  36.         </repository>           
  37.     </repositories>           
  38.     <pluginRepositories>           
  39.         <pluginRepository>           
  40.             <id>scala-tools.org</id>           
  41.             <name>Scala-tools Maven2 Repository</name>           
  42.             <url>http://scala-tools.org/repo-releases</url>           
  43.          </pluginRepository>          
  44.            
  45.     </pluginRepositories>           
  46.     <dependencyManagement>           
  47.         <dependencies>           
  48.             <dependency>           
  49.                  <groupId>org.springframework</groupId>          
  50.                 <artifactId>spring-context</artifactId>           
  51.            
  52.                 <version>${spring.version}</version>           
  53.              </dependency>          
  54.            
  55.              <dependency>          
  56.                 <groupId>org.springframework</groupId>           
  57.            
  58.                 <artifactId>spring-core</artifactId>           
  59.                 <version>${spring.version}</version>           
  60.             </dependency>           
  61.             <dependency>           
  62.                 <groupId>org.springframework</groupId>           
  63.                  <artifactId>spring-web</artifactId>          
  64.            
  65.                 <version>${spring.version}</version>           
  66.              </dependency>          
  67.            
  68.              <dependency>          
  69.                 <groupId>org.springframework</groupId>           
  70.            
  71.                  <artifactId>spring-orm</artifactId>          
  72.                 <version>${spring.version}</version>           
  73.            
  74.             </dependency>           
  75.         </dependencies>           
  76.     </dependencyManagement>           
  77.     <dependencies>           
  78.         <dependency>           
  79.             <groupId>com.h2database</groupId>           
  80.             <artifactId>h2</artifactId>           
  81.             <version>1.2.120</version>           
  82.         </dependency>           
  83.         <dependency>           
  84.              <groupId>org.hibernate</groupId>          
  85.             <artifactId>hibernate-entitymanager</artifactId>           
  86.            
  87.             <version>3.4.0.GA</version>           
  88.         </dependency>           
  89.         <dependency>           
  90.             <groupId>org.springframework</groupId>           
  91.             <artifactId>spring-orm</artifactId>           
  92.         </dependency>           
  93.         <dependency>           
  94.             <groupId>com.ttdev</groupId>           
  95.             <artifactId>wpt-core</artifactId>           
  96.             <version>1.5.2-SNAPSHOT</version>           
  97.             <scope>test</scope>           
  98.         </dependency>           
  99.         <dependency>           
  100.             <groupId>com.ttdev</groupId>           
  101.             <artifactId>wpt-runtime-spring</artifactId>           
  102.              <version>1.5.2-SNAPSHOT</version>          
  103.            
  104.         </dependency>           
  105.         <dependency>           
  106.              <groupId>javax</groupId>          
  107.             <artifactId>javaee-web-api</artifactId>           
  108.            
  109.             <version>6.0</version>           
  110.             <scope>provided</scope>           
  111.         </dependency>           
  112.         <dependency>           
  113.             <groupId>junit</groupId>           
  114.             <artifactId>junit</artifactId>           
  115.             <version>3.8.2</version>           
  116.             <scope>test</scope>           
  117.         </dependency>           
  118.         <dependency>           
  119.             <groupId>org.scala-lang</groupId>           
  120.             <artifactId>scala-library</artifactId>           
  121.             <version>2.8.0.RC3</version>           
  122.         </dependency>           
  123.         <dependency>           
  124.             <groupId>org.apache.wicket</groupId>           
  125.             <artifactId>wicket</artifactId>           
  126.             <version>1.4.9</version>           
  127.         </dependency>           
  128.         <dependency>           
  129.             <groupId>org.slf4j</groupId>           
  130.             <artifactId>slf4j-log4j12</artifactId>           
  131.             <version>1.4.2</version>           
  132.         </dependency>           
  133.     </dependencies>           
  134.     <build>           
  135.         <pluginManagement>           
  136.             <plugins>           
  137.                  <plugin>          
  138.                     <groupId>org.apache.maven.plugins</groupId>           
  139.            
  140.                     <artifactId>maven-compiler-plugin</artifactId>           
  141.                      <version>2.0.2</version>          
  142.            
  143.                 </plugin>           
  144.                 <plugin>           
  145.                      <groupId>org.scala-tools</groupId>          
  146.                     <artifactId>maven-scala-plugin</artifactId>           
  147.            
  148.                     <version>2.9.1</version>           
  149.                 </plugin>           
  150.             </plugins>           
  151.         </pluginManagement>           
  152.         <plugins>           
  153.             <plugin>           
  154.                  <groupId>org.apache.maven.plugins</groupId>          
  155.                 <artifactId>maven-compiler-plugin</artifactId>           
  156.            
  157.                 <executions>           
  158.                     <execution>           
  159.                         <phase>compile</phase>           
  160.                         <goals>           
  161.                             <goal>compile</goal>           
  162.                         </goals>           
  163.                     </execution>           
  164.                 </executions>           
  165.                 <configuration>           
  166.                     <source>1.6</source>           
  167.                     <target>1.6</target>           
  168.                 </configuration>           
  169.             </plugin>           
  170.             <plugin>           
  171.                 <groupId>org.apache.maven.plugins</groupId>           
  172.                  <artifactId>maven-war-plugin</artifactId>          
  173.            
  174.                 <version>2.1-beta-1</version>           
  175.                 <configuration>           
  176.                     <failOnMissingWebXml>false</failOnMissingWebXml>           
  177.                  </configuration>          
  178.            
  179.             </plugin>           
  180.             <plugin>           
  181.                  <groupId>org.scala-tools</groupId>          
  182.                 <artifactId>maven-scala-plugin</artifactId>           
  183.            
  184.                 <executions>           
  185.                     <execution>           
  186.                          <id>scala-compile-first</id>          
  187.                         <phase>process-resources</phase>           
  188.            
  189.                          <goals>          
  190.                             <goal>add-source</goal>           
  191.            
  192.                             <goal>compile</goal>           
  193.                         </goals>           
  194.                     </execution>           
  195.                     <execution>           
  196.                          <id>scala-test-compile</id>          
  197.                         <phase>process-test-resources</phase>           
  198.            
  199.                          <goals>          
  200.                             <goal>testCompile</goal>           
  201.            
  202.                         </goals>           
  203.                     </execution>           
  204.                 </executions>           
  205.             </plugin>           
  206.         </plugins>           
  207.         <finalName>myapp</finalName>           
  208.     </build>           
  209. </project>         

等待一段时间,IDEA将提示导入的项目是否需要更改。回答是肯定。

#p#

web.xml中设置

接下来,修改main/webapp/WEB-INF/web.xml。从而确立了Wicket的过滤器,打开Spring的JPA实体管理器和Spring监听器,进行Spring的初始化。

  1.  <?xml version="1.0" encoding="UTF-8"?>          
  2. <web-app version="3.0"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
  3.            
  4.          xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">           
  5.      <filter>          
  6.            
  7.          <filter-name>f2</filter-name>          
  8.         <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>           
  9.            
  10.     </filter>           
  11.     <filter>           
  12.          <filter-name>f1</filter-name>          
  13.         <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>           
  14.            
  15.          <init-param>          
  16.             <param-name>applicationClassName</param-name>           
  17.            
  18.             <param-value>com.foo.myapp.MyApp</param-value>           
  19.          </init-param>          
  20.            
  21.     </filter>           
  22.     <filter-mapping>           
  23.         <filter-name>f1</filter-name>           
  24.         <url-pattern>/app/*</url-pattern>           
  25.     </filter-mapping>           
  26.     <filter-mapping>           
  27.         <filter-name>f2</filter-name>           
  28.         <url-pattern>/*</url-pattern>           
  29.     </filter-mapping>           
  30.     <context-param>           
  31.         <param-name>contextConfigLocation</param-name>           
  32.         <param-value>classpath:/beans.xml</param-value>           
  33.     </context-param>           
  34.     <listener>           
  35.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>           
  36.      </listener>          
  37.            
  38. </web-app>           

#p#

设置Spring bean访问和调用的数据库

要定义这些Spring bean,创建main/resources的beans.xml以及一下的内容:

  1. <?xml version="1.0" encoding="UTF-8" ?>         
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"          
  3.           
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
  5.        xmlns:context="http://www.springframework.org/schema/context"          
  6.           
  7.         xmlns:tx="http://www.springframework.org/schema/tx"         
  8.        xsi:schemaLocation="http://www.springframework.org/schema/beans          
  9.           
  10.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd          
  11.          http://www.springframework.org/schema/context         
  12.           
  13.         http://www.springframework.org/schema/context/spring-context-3.0.xsd          
  14.          http://www.springframework.org/schema/tx         
  15.           
  16.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">          
  17.      <context:component-scan base-package="com.foo.myapp"/>         
  18.           
  19.     <bean id="emf"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">          
  20.         <property name="persistenceXmlLocation"value="classpath:/META-INF/my-persistence.xml"/>          
  21.      </bean>         
  22.     <bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">          
  23.           
  24.         <property name="entityManagerFactory" ref="emf"/>          
  25.      </bean>         
  26.           
  27.     <tx:annotation-driven />          
  28. </beans> 

读取数据库配置中persistence.xml文件。在这里,您将获得一个主目录中名为MyApp的H2数据库。因此,创建main/resources/META-INF文件的内容如下。

  1. <persistencexmlnspersistencexmlns="http://java.sun.com/xml/ns/persistence"          
  2.             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"           
  3.           
  4.             xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"           
  5.              version="2.0">          
  6.           
  7.    <persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">           
  8.         <provider>org.hibernate.ejb.HibernatePersistence</provider>          
  9.           
  10.         <properties>          
  11.            <propertynamepropertyname="hibernate.connection.driver_class"value="org.h2.Driver"/>           
  12.           
  13.            <property name="hibernate.connection.url"value="jdbc:h2:tcp://localhost/~/myapp"/>           
  14.             <propertynamepropertyname="hibernate.connection.username" value="sa"/>          
  15.           
  16.             <propertynamepropertyname="hibernate.connection.password" value=""/>          
  17.            <property name="hibernate.dialect"value="org.hibernate.dialect.H2Dialect"/>           
  18.           
  19.            <property name="hibernate.hbm2ddl.auto"value="create"/>           
  20.         </properties>          
  21.           
  22.    </persistence-unit>           
  23. /persistence>       

#p#

创建Wicket的页面

在这里,您将创建一个Wicket页面显示从数据库加载一些内容。因此,创建主/Scala文件夹,右键单击它并选择新建|Scala类。命名为我的页面类和投入com.foo.myapp包中。如下所示:

  1. package com.foo.myapp           
  2.            
  3. import org.apache.wicket.markup.html._           
  4. import basic.Label           
  5.  import list.{ListItem, ListView}          
  6. importorg.apache.wicket.spring.injection.annot.SpringBean           
  7.            
  8. import org.apache.wicket.model.CompoundPropertyModel           
  9.            
  10. class MyPage extends WebPage {           
  11.   @SpringBean           
  12.    var ps: ProductService = _          
  13.   val productListView = newListView[Product]("productListView", ps.getAll) {           
  14.            
  15.      def populateItem(item: ListItem[Product]) = {          
  16.       item.setModel(newCompoundPropertyModel[Product](item.getDefaultModelObject))           
  17.            
  18.       item.add(new Label("name"))           
  19.       item.add(new Label("price"))           
  20.     }           
  21.   }           
  22.   add(productListView)           
  23. }           
  24.  

请注意,在创建ProductService对象加载的文件之后使用。此外,请注意该字段被分配到一个下划线(_),Scala编译为不可初始化的,而是留在默认状态。在你将覆盖作为编译之前,执行Spring bean的页面构造。现在,创建在src/main/resources/com/foo/myapp的的mypage.html文件:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">           
  2. <html>           
  3.     <table border="1">           
  4.         <tr wicket:id="productListView">           
  5.             <td wicket:id="name"></td>           
  6.             <td wicket:id="price"></td>           
  7.         </tr>           
  8.     </table>           
  9. </html>           
  10.  

#p#

创建ProductService:

右键单击在in the src/main/scala文件夹中的com.foo.myapp包并选择New | Scala的类,然后选择创建ProductService:

  1. package com.foo.myapp           
  2.            
  3. import java.util.List           
  4.            
  5. trait ProductService {           
  6.   def getAll: List[Product]           
  7. }           
  8.  

这是接口。对创造一个DefaultProductService的Scala在同一个包内进行执行操作:

  1. package com.foo.myapp           
  2.            
  3. import javax.persistence.{PersistenceContext, EntityManager}           
  4. import org.springframework.stereotype.{Service}           
  5. importorg.springframework.transaction.annotation.Transactional           
  6.  importorg.springframework.beans.factory.annotation.Autowired          
  7.            
  8.            
  9. @Service           
  10. @Transactional           
  11. class DefaultProductService extends ProductService {           
  12.   @Autowired           
  13.   var products: Products = _           
  14.            
  15.   def getAll = products.getAll           
  16. }           
  17.  

请注意,把它使用类型文件命名为DAO。

创建文件的DAO

要创建的DAO,创建一个名为在同一文件夹:

  1. package com.foo.myapp           
  2.            
  3. import java.util.List           
  4.            
  5. trait Products {           
  6.    def getAll: List[Product]           
  7. }           
  8.  

然后创建在同一文件夹中的执行Scala的DefaultProducts文件:

  1. package com.foo.myapp           
  2.            
  3. import javax.persistence.{PersistenceContext, EntityManager}           
  4. import org.springframework.stereotype.Repository           
  5. import java.util.List           
  6.            
  7. @Repository           
  8. class DefaultProducts extends Products {           
  9.   @PersistenceContext           
  10.   var em: EntityManager = _           
  11.            
  12.   def getAll = {           
  13.     em.createQuery("select p from Product p").getResultList.asInstanceOf[List[Product]]           
  14.   }           
  15. }           
  16.  

#p#

创建entity程序

创建entity程序并将其映射到数据库:

  1. package com.foo.myapp           
  2.            
  3. import javax.persistence.{GeneratedValue, Id, Entity}           
  4.            
  5. @Entity           
  6. class Product {           
  7.   @Id           
  8.   @GeneratedValue           
  9.   var id: Long = _           
  10.   var name: String = _           
  11.   var price: Double = _           
  12. }           
  13.  

创建Wicket的应用程序

***,创建在同一个MyApp的Scala程序:

  1. package com.foo.myapp           
  2.            
  3.  import org.apache.wicket.protocol.http.WebApplication          
  4. importcom.ttdev.wicketpagetest.MockableSpringBeanInjector           
  5.            
  6.            
  7. class MyApp extends WebApplication {           
  8.   def getHomePage = classOf[MyPage]           
  9.            
  10.    override def init = {          
  11.     MockableSpringBeanInjector.installInjector(this)           
  12.            
  13.   }           
  14. }           
  15.  

在这里,您指定为首页我的目录,植入Spring Bean中。

运行应用程序

随着IntelliJ IDEA的版本不断改进,您将嵌入到应用服务器运行您的应用程序。这非常容易,在刚刚创建的测试com.foo.myapp的ManualTest在刚刚创建的测试com.foo.myapp包一类ManualTest/scala文件(不是main/scala文件),以下内容是:

  1. package com.foo.myapp           
  2.            
  3. importcom.ttdev.wicketpagetest.{WebAppJettyConfiguration, WicketAppJettyLauncher}           
  4.            
  5.            
  6. object ManualTest {           
  7.   def main(args: Array[String]) {           
  8.     val l = new WicketAppJettyLauncher           
  9.     l.startAppInJetty(new WebAppJettyConfiguration)           
  10.   }           
  11. }           
  12.  

要运行的应用程序,首先需要运行数据库服务器上的H2程序。所以,在http://www.h2database.com下载和解压。然后变成h2/bin和运行h2.bat(或在Linux上h2.sh)。ManualTest是在IDEA的编辑器上,在IDEA选择运行DEA。它将运行ManualTest,Jetty的运行您的应用程序。

为了测试它,请尝试通过在浏览器http://localhost:8888/app。

 不知道各位感觉如何。感谢各位支持!谢谢!

原文地址:agileskills2.org/blog/2010/06/19/getting-started-with-scala-spring-hibernate-wicket/

原文名:Getting started with Scala, Spring, Hibernate & Wicket

 

【51CTO译稿,非经授权谢绝转载,合作媒体转载请注明原文出处及作者!】

【编辑推荐】

  1. Scala:Java+函数式=后函数式?
  2. 对象函数式编程 Scala简史
  3. 与Scala的***次亲密接触 Java如影相随

 

责任编辑:佚名 来源: 51CTO.com编译
相关推荐

2011-07-01 11:02:30

EnyowebOShello world

2011-07-01 10:52:59

EnyowebOS 3.0 S

2022-11-11 10:56:37

2009-06-19 16:05:39

时间类型Struts

2011-06-16 16:41:20

Qt MeeGo SDK

2010-10-15 09:52:00

XephyrMeeGoQt

2009-06-01 10:47:32

jboss seam例jboss seam开jboss seam

2009-06-23 15:51:00

JSF框架

2011-04-22 11:24:13

mootools

2009-11-16 17:04:46

Inside Scal

2021-08-19 11:51:37

Commvault

2010-08-03 09:15:05

ScalaSpring

2021-07-16 09:54:49

鸿蒙HarmonyOS应用

2020-02-20 15:19:56

JavaSQL注入

2009-09-28 11:01:39

从Java走进Scal

2009-08-21 16:17:25

ScalaTwitter API

2009-07-22 08:57:49

Scalafinal

2014-07-21 17:48:09

PhoneGapCordovaHtml5

2021-10-14 09:53:38

鸿蒙HarmonyOS应用

2023-06-12 15:33:52

Scalafor循环语句
点赞
收藏

51CTO技术栈公众号