【51CTO编译】51CTO曾经在“与Scala的***次亲密接触 Java如影相随”为大家讲过Java于Scala。下面是一个教程文件是我在如今***或者说***的Java框架——Spring、Hibernate/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的汇编语言:
- <project xmlnsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.foo</groupId>
- <artifactId>myapp</artifactId>
- <packaging>war</packaging>
- <version>1.0-SNAPSHOT</version>
- <name>myapp Java EE 6 Webapp</name>
- <url>http://maven.apache.org</url>
- <properties>
- <spring.version>3.0.3.RELEASE</spring.version>
- </properties>
- <repositories>
- <repository>
- <id>java.net2</id>
- <name>Repository hosting the jee6 artifacts</name>
- <url>http://download.java.net/maven/2</url>
- </repository>
- <repository>
- <id>scala-tools.org</id>
- <name>Scala-tools Maven2 Repository</name>
- <url>http://scala-tools.org/repo-releases</url>
- </repository>
- <repository>
- <id>wpt-release</id>
- <url>http://wicketpagetest.sourceforge.net/m2-repo/releases</url>
- </repository>
- <repository>
- <id>wpt-snapshot</id>
- <url>http://wicketpagetest.sourceforge.net/m2-repo/snapshots</url>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>scala-tools.org</id>
- <name>Scala-tools Maven2 Repository</name>
- <url>http://scala-tools.org/repo-releases</url>
- </pluginRepository>
- </pluginRepositories>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>com.h2database</groupId>
- <artifactId>h2</artifactId>
- <version>1.2.120</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>3.4.0.GA</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- </dependency>
- <dependency>
- <groupId>com.ttdev</groupId>
- <artifactId>wpt-core</artifactId>
- <version>1.5.2-SNAPSHOT</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>com.ttdev</groupId>
- <artifactId>wpt-runtime-spring</artifactId>
- <version>1.5.2-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-web-api</artifactId>
- <version>6.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.2</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.scala-lang</groupId>
- <artifactId>scala-library</artifactId>
- <version>2.8.0.RC3</version>
- </dependency>
- <dependency>
- <groupId>org.apache.wicket</groupId>
- <artifactId>wicket</artifactId>
- <version>1.4.9</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.4.2</version>
- </dependency>
- </dependencies>
- <build>
- <pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.0.2</version>
- </plugin>
- <plugin>
- <groupId>org.scala-tools</groupId>
- <artifactId>maven-scala-plugin</artifactId>
- <version>2.9.1</version>
- </plugin>
- </plugins>
- </pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <executions>
- <execution>
- <phase>compile</phase>
- <goals>
- <goal>compile</goal>
- </goals>
- </execution>
- </executions>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-war-plugin</artifactId>
- <version>2.1-beta-1</version>
- <configuration>
- <failOnMissingWebXml>false</failOnMissingWebXml>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.scala-tools</groupId>
- <artifactId>maven-scala-plugin</artifactId>
- <executions>
- <execution>
- <id>scala-compile-first</id>
- <phase>process-resources</phase>
- <goals>
- <goal>add-source</goal>
- <goal>compile</goal>
- </goals>
- </execution>
- <execution>
- <id>scala-test-compile</id>
- <phase>process-test-resources</phase>
- <goals>
- <goal>testCompile</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- <finalName>myapp</finalName>
- </build>
- </project>
等待一段时间,IDEA将提示导入的项目是否需要更改。回答是肯定。
#p#
web.xml中设置
接下来,修改main/webapp/WEB-INF/web.xml。从而确立了Wicket的过滤器,打开Spring的JPA实体管理器和Spring监听器,进行Spring的初始化。
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <filter>
- <filter-name>f2</filter-name>
- <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
- </filter>
- <filter>
- <filter-name>f1</filter-name>
- <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
- <init-param>
- <param-name>applicationClassName</param-name>
- <param-value>com.foo.myapp.MyApp</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>f1</filter-name>
- <url-pattern>/app/*</url-pattern>
- </filter-mapping>
- <filter-mapping>
- <filter-name>f2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:/beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
#p#
设置Spring bean访问和调用的数据库
要定义这些Spring bean,创建main/resources的beans.xml以及一下的内容:
- <?xml version="1.0" encoding="UTF-8" ?>
- <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <context:component-scan base-package="com.foo.myapp"/>
- <bean id="emf"class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="persistenceXmlLocation"value="classpath:/META-INF/my-persistence.xml"/>
- </bean>
- <bean id="transactionManager"class="org.springframework.orm.jpa.JpaTransactionManager">
- <property name="entityManagerFactory" ref="emf"/>
- </bean>
- <tx:annotation-driven />
- </beans>
读取数据库配置中persistence.xml文件。在这里,您将获得一个主目录中名为MyApp的H2数据库。因此,创建main/resources/META-INF文件的内容如下。
- <persistencexmlnspersistencexmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
- version="2.0">
- <persistence-unit name="myapp" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <properties>
- <propertynamepropertyname="hibernate.connection.driver_class"value="org.h2.Driver"/>
- <property name="hibernate.connection.url"value="jdbc:h2:tcp://localhost/~/myapp"/>
- <propertynamepropertyname="hibernate.connection.username" value="sa"/>
- <propertynamepropertyname="hibernate.connection.password" value=""/>
- <property name="hibernate.dialect"value="org.hibernate.dialect.H2Dialect"/>
- <property name="hibernate.hbm2ddl.auto"value="create"/>
- </properties>
- </persistence-unit>
- /persistence>
#p#
创建Wicket的页面
在这里,您将创建一个Wicket页面显示从数据库加载一些内容。因此,创建主/Scala文件夹,右键单击它并选择新建|Scala类。命名为我的页面类和投入com.foo.myapp包中。如下所示:
- package com.foo.myapp
- import org.apache.wicket.markup.html._
- import basic.Label
- import list.{ListItem, ListView}
- importorg.apache.wicket.spring.injection.annot.SpringBean
- import org.apache.wicket.model.CompoundPropertyModel
- class MyPage extends WebPage {
- @SpringBean
- var ps: ProductService = _
- val productListView = newListView[Product]("productListView", ps.getAll) {
- def populateItem(item: ListItem[Product]) = {
- item.setModel(newCompoundPropertyModel[Product](item.getDefaultModelObject))
- item.add(new Label("name"))
- item.add(new Label("price"))
- }
- }
- add(productListView)
- }
请注意,在创建ProductService对象加载的文件之后使用。此外,请注意该字段被分配到一个下划线(_),Scala编译为不可初始化的,而是留在默认状态。在你将覆盖作为编译之前,执行Spring bean的页面构造。现在,创建在src/main/resources/com/foo/myapp的的mypage.html文件:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <table border="1">
- <tr wicket:id="productListView">
- <td wicket:id="name"></td>
- <td wicket:id="price"></td>
- </tr>
- </table>
- </html>
#p#
创建ProductService:
右键单击在in the src/main/scala文件夹中的com.foo.myapp包并选择New | Scala的类,然后选择创建ProductService:
- package com.foo.myapp
- import java.util.List
- trait ProductService {
- def getAll: List[Product]
- }
这是接口。对创造一个DefaultProductService的Scala在同一个包内进行执行操作:
- package com.foo.myapp
- import javax.persistence.{PersistenceContext, EntityManager}
- import org.springframework.stereotype.{Service}
- importorg.springframework.transaction.annotation.Transactional
- importorg.springframework.beans.factory.annotation.Autowired
- @Service
- @Transactional
- class DefaultProductService extends ProductService {
- @Autowired
- var products: Products = _
- def getAll = products.getAll
- }
请注意,把它使用类型文件命名为DAO。
创建文件的DAO
要创建的DAO,创建一个名为在同一文件夹:
- package com.foo.myapp
- import java.util.List
- trait Products {
- def getAll: List[Product]
- }
然后创建在同一文件夹中的执行Scala的DefaultProducts文件:
- package com.foo.myapp
- import javax.persistence.{PersistenceContext, EntityManager}
- import org.springframework.stereotype.Repository
- import java.util.List
- @Repository
- class DefaultProducts extends Products {
- @PersistenceContext
- var em: EntityManager = _
- def getAll = {
- em.createQuery("select p from Product p").getResultList.asInstanceOf[List[Product]]
- }
- }
#p#
创建entity程序
创建entity程序并将其映射到数据库:
- package com.foo.myapp
- import javax.persistence.{GeneratedValue, Id, Entity}
- @Entity
- class Product {
- @Id
- @GeneratedValue
- var id: Long = _
- var name: String = _
- var price: Double = _
- }
创建Wicket的应用程序
***,创建在同一个MyApp的Scala程序:
- package com.foo.myapp
- import org.apache.wicket.protocol.http.WebApplication
- importcom.ttdev.wicketpagetest.MockableSpringBeanInjector
- class MyApp extends WebApplication {
- def getHomePage = classOf[MyPage]
- override def init = {
- MockableSpringBeanInjector.installInjector(this)
- }
- }
在这里,您指定为首页我的目录,植入Spring Bean中。
运行应用程序
随着IntelliJ IDEA的版本不断改进,您将嵌入到应用服务器运行您的应用程序。这非常容易,在刚刚创建的测试com.foo.myapp的ManualTest在刚刚创建的测试com.foo.myapp包一类ManualTest/scala文件(不是main/scala文件),以下内容是:
- package com.foo.myapp
- importcom.ttdev.wicketpagetest.{WebAppJettyConfiguration, WicketAppJettyLauncher}
- object ManualTest {
- def main(args: Array[String]) {
- val l = new WicketAppJettyLauncher
- l.startAppInJetty(new WebAppJettyConfiguration)
- }
- }
要运行的应用程序,首先需要运行数据库服务器上的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译稿,非经授权谢绝转载,合作媒体转载请注明原文出处及作者!】
【编辑推荐】