spring4.0之八:Groovy DSL
4.0的一个重要特征就是完全支持Groovy,Groovy是Spring主导的一门基于JVM的脚本语言(动态语言)。在spring 2.x,脚本语言通过 Java scripting engine在Spring中得到支持。而在4.0中,Groovy的变得更重要,Groovy可以替换xml和注解用来作为bean配置。
要使用Groovy,首先用maven下载Groovy的包,pom.xml文件中添加:
- <dependency>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy-all</artifactId>
- <version>2.1.8</version>
- </dependency>
下面使用xml,java annotation,groovy dsl实现相同功能的不同配置方式比较
XML
- <jdbc:embedded-database id="dataSource" type="H2">
- </jdbc:embedded-database>
- <bean
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
- id="entityManagerFactory">
- <property name="persistenceUnitName" value="persistenceUnit" />
- <property name="dataSource" ref="dataSource" />
- <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property>
- <property name="packagesToScan">
- <array>
- <value>com.hantsylabs.example.spring.model</value>
- </array>
- </property>
- <property name="jpaProperties">
- <value>
- hibernate.format_sql=true
- hibernate.show_sql=true
- hibernate.hbm2ddl.auto=create
- </value>
- </property>
- </bean>
- <bean class="org.springframework.orm.jpa.JpaTransactionManager"
- id="transactionManager">
- <property name="entityManagerFactory" ref="entityManagerFactory" />
- </bean>
Annotation
- @Configuration
- @ComponentScan(basePackages = { "com.hantsylabs.example.spring.dao","com.hantsylabs.example.spring.jpa" })
- @EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
- public class JpaConfig {
- @Bean
- public DataSource dataSource() {
- return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
- }
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
- LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
- emf.setDataSource(dataSource());
- emf.setPackagesToScan("com.hantsylabs.example.spring.model");
- emf.setPersistenceProvider(new HibernatePersistence());
- emf.setJpaProperties(jpaProperties());
- return emf;
- }
- private Properties jpaProperties() {
- Properties extraProperties = new Properties();
- extraProperties.put("hibernate.format_sql", "true");
- extraProperties.put("hibernate.show_sql", "true");
- extraProperties.put("hibernate.hbm2ddl.auto", "create");
- return extraProperties;
- }
- @Bean
- public PlatformTransactionManager transactionManager() {
- return new JpaTransactionManager(entityManagerFactory().getObject());
- }
- }
Groovy DSL
- import org.apache.commons.dbcp.BasicDataSource
- import org.springframework.orm.jpa.JpaTransactionManager
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
- import com.hantsylabs.example.spring.jpa.JpaConferenceDaoImpl
- beans {
- dataSource(BasicDataSource) {
- driverClassName = "org.h2.Driver"
- url = "jdbc:h2:mem:spring4-sandbox"
- username = "sa"
- password = ""
- }
- entityManagerFactory(LocalContainerEntityManagerFactoryBean){
- persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"
- dataSource=dataSource
- persistenceUnitName="persistenceUnit"
- packagesToScan=["com.hantsylabs.example.spring.model"]
- jpaProperties=[
- "hibernate.format_sql":"true",
- "hibernate.show_sql":"true",
- "hibernate.hbm2ddl.auto":"create"
- ]
- }
- transactionManager(JpaTransactionManager){
- entityManagerFactory=entityManagerFactory
- }
- conferenceDao(JpaConferenceDaoImpl){
- }
- }
spring4.0之八:Groovy DSL的更多相关文章
- Spring4.0支持Groovy配置
介绍 前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持.因为临时还没有很深入的研究.所以举个小样例来说明一下怎样支持Groovy配置. package shuai.s ...
- Spring4.0系列9-websocket简单应用
http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...
- Spring-Context之三:使用XML和Groovy DSL配置Bean
在第一讲中显示了如何使用注解配置bean,其实这是Spring3引进的特性,Spring2使用的是XML的方式来配置Bean,那时候漫天的XML文件使得Spring有着配置地狱的称号.Spring也一 ...
- [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合
原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...
- Spring4.0编程式定时任务配置
看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...
- Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用]
Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用] tip:只需要配置xml文件即可 1.第三方依赖包的引入 <properties> <project.bu ...
- [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...
- 项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度
前言 系列文章:[传送门] 项目需求: http://www.cnblogs.com/Alandre/p/3733249.html 上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备 ...
- 项目ITP(五) spring4.0 整合 Quartz 实现任务调度
前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...
随机推荐
- HDU 1069:Monkey and Banana(DP)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- opencv3中SurfFeatureDetector、SurfDescriptorExtractor、BruteForceMatcher的使用
opencv2中SurfFeatureDetector.SurfDescriptorExtractor.BruteForceMatcher在opencv3中发生了改变.具体如何完成特征点匹配呢?示例如 ...
- codeforce 804B Minimum number of steps
cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each ...
- POJ1417 True Liars
题意 Language:Default True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6392 Accep ...
- centos7配置lamp成功安装过
linux+apache+mysql/mariadb+php 首先apache的安装: yum install httpd 接着mysql/mariadb的安装: yum install mysql ...
- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方案
导入commons-logging-1.2.jar辅助类包即可. 报错提示: Exception in thread "main" java.lang.NoClassDefFoun ...
- <--------------------------StringBuffer的常用方法------------------------------>
StringBuffer定义: 是可变字符数组,是线程安全的可变字符序列. StringBuffer和String的区别: String是一个不可变的字符序列. 实例: public class St ...
- 100 webhook implementations
转自: https://streamdata.io/blog/100-webhook-implementations/ 很不错的整理 What is the scope of the event-d ...
- pipenv 方便的python 开发工作流工具
pipenv 将 composer.bundler.npm.yarn.cargo 等比较方便的包管理工具添加到了python 语言中,可以 帮助我们自动的管理virtualenv ,同时可以方便的从p ...
- 使用Managed Extensibility Framework方便的扩展应用程序
概述 Managed Extensibility Framework(MEF)是.NET平台下的一个扩展性管理框架,它是一系列特性的集合,包括依赖注入(DI)以及Duck Typing等.MEF为开发 ...