Spring4.0系列8-Groovy DSL
Spring4.0系列1-新特性
Spring4.0系列2-环境搭建
Spring4.0系列3-@RestController
Spring4.0系列4-Meta Annotation(元注解)
Spring4.0系列5-@Conditional
Spring4.0系列6-Generic Qualifier(泛型限定)
Spring4.0系列7-Ordering Autowired Collections
Spring4.0系列8-Groovy DSL
Spring4.0系列9-websocket简单应用
更多正在编写中。。。
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){ } }
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。