4.0的一个重要特征就是完全支持Groovy,Groovy是Spring主导的一门基于JVM的脚本语言(动态语言)。在spring 2.x,脚本语言通过 Java scripting engine在Spring中得到支持。而在4.0中,Groovy的变得更重要,Groovy可以替换xml和注解用来作为bean配置。

要使用Groovy,首先用maven下载Groovy的包,pom.xml文件中添加:

  1. <dependency>
  2. <groupId>org.codehaus.groovy</groupId>
  3. <artifactId>groovy-all</artifactId>
  4. <version>2.1.8</version>
  5. </dependency>

下面使用xml,java annotation,groovy dsl实现相同功能的不同配置方式比较

XML

  1. <jdbc:embedded-database id="dataSource" type="H2">
  2. </jdbc:embedded-database>
  3. <bean
  4. class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  5. id="entityManagerFactory">
  6. <property name="persistenceUnitName" value="persistenceUnit" />
  7. <property name="dataSource" ref="dataSource" />
  8. <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property>
  9. <property name="packagesToScan">
  10. <array>
  11. <value>com.hantsylabs.example.spring.model</value>
  12. </array>
  13. </property>
  14. <property name="jpaProperties">
  15. <value>
  16. hibernate.format_sql=true
  17. hibernate.show_sql=true
  18. hibernate.hbm2ddl.auto=create
  19. </value>
  20. </property>
  21. </bean>
  22. <bean class="org.springframework.orm.jpa.JpaTransactionManager"
  23. id="transactionManager">
  24. <property name="entityManagerFactory" ref="entityManagerFactory" />
  25. </bean>

Annotation

  1. @Configuration
  2. @ComponentScan(basePackages = { "com.hantsylabs.example.spring.dao","com.hantsylabs.example.spring.jpa" })
  3. @EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
  4. public class JpaConfig {
  5. @Bean
  6. public DataSource dataSource() {
  7. return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
  8. }
  9. @Bean
  10. public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  11. LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
  12. emf.setDataSource(dataSource());
  13. emf.setPackagesToScan("com.hantsylabs.example.spring.model");
  14. emf.setPersistenceProvider(new HibernatePersistence());
  15. emf.setJpaProperties(jpaProperties());
  16. return emf;
  17. }
  18. private Properties jpaProperties() {
  19. Properties extraProperties = new Properties();
  20. extraProperties.put("hibernate.format_sql", "true");
  21. extraProperties.put("hibernate.show_sql", "true");
  22. extraProperties.put("hibernate.hbm2ddl.auto", "create");
  23. return extraProperties;
  24. }
  25. @Bean
  26. public PlatformTransactionManager transactionManager() {
  27. return new JpaTransactionManager(entityManagerFactory().getObject());
  28. }
  29. }

Groovy DSL

  1. import org.apache.commons.dbcp.BasicDataSource
  2. import org.springframework.orm.jpa.JpaTransactionManager
  3. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
  4. import com.hantsylabs.example.spring.jpa.JpaConferenceDaoImpl
  5. beans {
  6. dataSource(BasicDataSource) {
  7. driverClassName = "org.h2.Driver"
  8. url = "jdbc:h2:mem:spring4-sandbox"
  9. username = "sa"
  10. password = ""
  11. }
  12. entityManagerFactory(LocalContainerEntityManagerFactoryBean){
  13. persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"
  14. dataSource=dataSource
  15. persistenceUnitName="persistenceUnit"
  16. packagesToScan=["com.hantsylabs.example.spring.model"]
  17. jpaProperties=[
  18. "hibernate.format_sql":"true",
  19. "hibernate.show_sql":"true",
  20. "hibernate.hbm2ddl.auto":"create"
  21. ]
  22. }
  23. transactionManager(JpaTransactionManager){
  24. entityManagerFactory=entityManagerFactory
  25. }
  26. conferenceDao(JpaConferenceDaoImpl){
  27. }
  28. }

最新文章

  1. IE7 自动为文件路径添加域名
  2. Struts 2的OGNL的根对象
  3. mybatis 如何使用乐观锁
  4. 微软注册dll在dotnet开发时起到缓存的作用
  5. POJ 1947 Rebuilding Roads
  6. 导航栏4种效果---原生js
  7. git Unstaged changes after reset
  8. JavaScript 类型判断的那些事
  9. mssql 2008 失败 需要重新启动计算机 的解决办法
  10. SQL中多条件查询括号的用途
  11. 文件读写io操作范例
  12. Appuim的安装步骤
  13. OrchardCore 如何实现模块化( Modular )和 Multi-Tenancy
  14. nginx的location配置root、alias用法和区别
  15. JDBCUtils——DBCP
  16. PIL: 建立一个GIF图
  17. Haproxy 安装初体验
  18. 天使玩偶:CDQ分治
  19. Python Django框架笔记(一):安装及创建项目
  20. 通俗理解RxJS(一)

热门文章

  1. c++输入输出流加速器
  2. sudo操作
  3. spring IOC简单分析
  4. Singer 学习三 使用Singer进行mongodb 2 postgres 数据转换
  5. python基础教程_学习笔记9:抽象
  6. php 数组排序 按照某字段
  7. 编写一个函数实现n^k,使用递归实现
  8. Apache和Nginx负载均衡集群及测试分析
  9. day 34 线程队列 线程池 协程 Greenlet \Gevent 模块
  10. Spring Cloud 与 Dubbo、Spring Cloud 与 Docker、Spring Cloud 与 Kubernetes 比较