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. }

spring4.0之八:Groovy DSL的更多相关文章

  1. Spring4.0支持Groovy配置

    介绍 前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持.因为临时还没有很深入的研究.所以举个小样例来说明一下怎样支持Groovy配置. package shuai.s ...

  2. Spring4.0系列9-websocket简单应用

    http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...

  3. Spring-Context之三:使用XML和Groovy DSL配置Bean

    在第一讲中显示了如何使用注解配置bean,其实这是Spring3引进的特性,Spring2使用的是XML的方式来配置Bean,那时候漫天的XML文件使得Spring有着配置地狱的称号.Spring也一 ...

  4. [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

    原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...

  5. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  6. Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用]

    Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用] tip:只需要配置xml文件即可 1.第三方依赖包的引入 <properties> <project.bu ...

  7. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...

  8. 项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度

    前言 系列文章:[传送门] 项目需求: http://www.cnblogs.com/Alandre/p/3733249.html 上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备 ...

  9. 项目ITP(五) spring4.0 整合 Quartz 实现任务调度

    前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...

随机推荐

  1. HDU 1686:Oulipo(KMP模板,子串出现次数)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. PTA——念数字

    PTA 7-30 念数字 #include<stdio.h> #include<stdlib.h> #define N 50 int main() { ] = {"l ...

  3. 系统间通信——RPC架构设计

    架构设计:系统间通信(10)——RPC的基本概念 1.概述经过了详细的信息格式.网络IO模型的讲解,并且通过JAVA RMI的讲解进行了预热.从这篇文章开始我们将进入这个系列博文的另一个重点知识体系的 ...

  4. python绝对路径相对路径函数

    绝对路径 os.path.abspath("文件名")  参数可为“”即当前路径 相对路径 os.path.dirname("文件名")   参数可为“”即当前 ...

  5. C++学习(十九)(C语言部分)之 指针3

    复习1.一级指针 int*p 指向int的指针 赋值 int x; p=&x;// *p=2; 指针指向的谁 解引用之后就是谁2.内存四区 堆区 需要自己手动申请内存 自己释放 (malloc ...

  6. poj 2155 B - Matrix 二维树状数组

    #include<iostream> #include<string> #include<string.h> #include<cstdio> usin ...

  7. struts2的国际化i18n

    先来例子,普通的读取配置文件中不同语言信息,一个测试类,一份中文配置文件,一份英文配置文件 中文配置文件,书写中文“欢迎”,myelipse自动转码 public class Readi18n { p ...

  8. js实现一条抛物线

    抛物线运动解释: 以右开口为例,根据公式  y^2 = 2px .确定p的值,已知x求y. <!DOCTYPE html> <html> <head> <me ...

  9. MySQL Transaction--MySQL与SQL Server在可重复读事务隔离级别上的差异

    MySQL和SQL Server两种数据库在REPEATABLE-READ事务隔离级别实现方式不同,导致使用上也存在差异. 在MySQL中,默认使用REPEATABLE-READ事务隔离级别,MySQ ...

  10. Using C++ new() placement in embedded system

    For new(), there are three definition in C++11, which are listed below. throwing (1) void* operator ...