项目代码 http://git.oschina.net/for-1988/Simples
Spring Scala 项目是Spring团队提供的,为了简化在 Scala 应用中使用 Spring 框架。咱们相信不少 Spring 用户想尝试 Scala,但并不像放弃他们在 Spring 框架上的积累,这个项目就是为这些人准备的。目前的版本已经1.0.0.RC1,因此打算尝试一下。Spring Scala项目目前是基于spring 3.2.4.RELEASE 版本开发的 java
scala的环境以及sbt等相关工具的搭建,这里就不详细描述了,网上有很多资料。下面是工程的build.sbt文件的配置 mysql
name := "Spring-Scala-Simple" version := "1.0" scalaVersion := "2.10.4" scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") resolvers ++= Seq( "SpringSource Milestone Repository" at "http://repo.springsource.org/milestone/") libraryDependencies ++= { val springscala = "1.0.0.RC1" val springV = "3.2.4.RELEASE" Seq( "org.springframework.scala" %% "spring-scala" % springscala, "org.springframework" % "spring-jdbc" % springV, "commons-dbcp" % "commons-dbcp" % "1.4", "mysql" % "mysql-connector-java" % "5.1.26", "aspectj" % "aspectjweaver" % "1.5.4", "org.scalatest" %% "scalatest" % "2.0" % "test", "junit" % "junit" % "4.11" % "test") }
Spring Scala中对spring的配置提供一种经过编码实现FunctionalConfiguration接口来配置Bean的方式,固然也支持xml的方式。咱们这里仍然采用的是xml的方法,由于我没有找到FunctionalConfiguration中如何去配置component-scan。 git
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:c="http://www.springframework.org/schema/c" xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.topteam.services"></context:component-scan> <context:component-scan base-package="com.topteam.dao"></context:component-scan> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:app.properties</value> </property> </bean> <!-- 勤务数据库配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/Simples" /> <property name="username" value="root" /> <property name="password" value="11111" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"> </property> </bean> <tx:advice id="txAdviceDuty" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" propagation="REQUIRED"/> <tx:method name="find*" read-only="true" propagation="REQUIRED"/> <tx:method name="login" read-only="false" propagation="REQUIRED"/> <tx:method name="exist*" read-only="true" propagation="REQUIRED"/> <tx:method name="query*" read-only="false" propagation="NOT_SUPPORTED"/> <tx:method name="*" read-only="false" propagation="REQUIRED" rollback-for="Exception"/> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:pointcut id="txPointcutDuty" expression="execution(* com.topteam.services..impl..*.*(..))"/> <aop:advisor advice-ref="txAdviceDuty" pointcut-ref="txPointcutDuty" order="1"/> </aop:config> <bean id="jack" class="com.topteam.bean.Person" c:firstName="Jack" c:lastName="Doe" /> <bean id="jane" class="com.topteam.bean.Person" c:firstName="Jane" c:lastName="Doe" /> <bean id="john" class="com.topteam.bean.Person" c:firstName="John" c:lastName="Doe"> <property name="father" ref="jack" /> <property name="mother" ref="jane" /> </bean> </beans>
这里能够配置信息跟之前没有任何区别,若是你的xml中的bean配置须要用到scala的集合。那么须要在xml文件中添加下面这段配置 spring
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="propertyEditorRegistrars"> <bean class="org.springframework.scala.beans.propertyeditors.ScalaEditorRegistrar"/> </property> </bean>
这里配置了数据源、事务以及一些咱们本身编写的bean。 sql
1.编写一个scala的bean shell
class Person(val firstName: String, val lastName: String) { var father: Person = _ var mother: Person = _ }
2.经过配置注入Bean 数据库
<bean id="jack" class="com.topteam.bean.Person" c:firstName="Jack" c:lastName="Doe" /> <bean id="jane" class="com.topteam.bean.Person" c:firstName="Jane" c:lastName="Doe" /> <bean id="john" class="com.topteam.bean.Person" c:firstName="John" c:lastName="Doe"> <property name="father" ref="jack" /> <property name="mother" ref="jane" /> </bean>
3.经过注解注入Bean express
@Service class PersonService { @Autowired @Qualifier("john") var john:Person = _ }4.单元测试,这里咱们简单的写了个单元测试的父类
trait SpringTestContext { lazy val applicationContext =new ClassPathXmlApplicationContext("classpath:spring-context.xml") }
class SimpleTest extends SpringTestContext{ @Test def beanTest(){ val person = applicationContext.getBean("john",classOf[Person]) println(person.father.firstName) // "Jack" } }
Spring Scala 中目前提供了JdbcTemplate、JmsTemplate、RestTemplate、TransactionTemplate。这块的目前的文档都没有,因此这边只是试着用了用JdbcTemplate。目前对crud基本没有作任何封装,只是用了JdbcTemplate提供的方法。因为Scala的一些语法特性,这块能够作更好的封装。 apache
trait BaseDao { implicit def dataSource: DataSource lazy val jdbcTemplate = new JdbcTemplate(dataSource) }
@Repository class PersonDao extends BaseDao { @Autowired @Qualifier("dataSource") var ds: DataSource = _ implicit lazy val dataSource = ds def findAllPerson: Seq[Person] = { def toPerson(rs: ResultSet, i: Int): Person = new Person(rs.getString(1), rs.getString(2)) jdbcTemplate.queryAndMap("select * from Person")(toPerson) } def savePerson(person: Person) = { jdbcTemplate.updateWithSetter("insert into Person(firstName, LastName) values(?,?)")(ps => { ps.setString(1, person.firstName) ps.setString(2, person.lastName) }) } }