zoukankan      html  css  js  c++  java
  • 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文件中添加:

    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

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

    Annotation

    Java代码  收藏代码
    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.   
    6.  @Bean  
    7.  public DataSource dataSource() {  
    8.   return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();  
    9.  }  
    10.   
    11.  @Bean  
    12.  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {  
    13.   LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();  
    14.   emf.setDataSource(dataSource());  
    15.   emf.setPackagesToScan("com.hantsylabs.example.spring.model");  
    16.   emf.setPersistenceProvider(new HibernatePersistence());  
    17.   emf.setJpaProperties(jpaProperties());  
    18.   return emf;  
    19.  }  
    20.   
    21.  private Properties jpaProperties() {  
    22.   Properties extraProperties = new Properties();  
    23.   extraProperties.put("hibernate.format_sql", "true");  
    24.   extraProperties.put("hibernate.show_sql", "true");  
    25.   extraProperties.put("hibernate.hbm2ddl.auto", "create");  
    26.   return extraProperties;  
    27.  }  
    28.   
    29.  @Bean  
    30.  public PlatformTransactionManager transactionManager() {  
    31.   return new JpaTransactionManager(entityManagerFactory().getObject());  
    32.  }  
    33.   
    34. }  

    Groovy DSL

    Java代码  收藏代码
    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.   
    6. beans {  
    7.      dataSource(BasicDataSource) {     
    8.          driverClassName = "org.h2.Driver"  
    9.          url = "jdbc:h2:mem:spring4-sandbox"  
    10.          username = "sa"  
    11.          password = ""  
    12.      }  
    13.     
    14.   entityManagerFactory(LocalContainerEntityManagerFactoryBean){  
    15.    persistenceProviderClass="org.hibernate.ejb.HibernatePersistence"  
    16.    dataSource=dataSource  
    17.    persistenceUnitName="persistenceUnit"  
    18.    packagesToScan=["com.hantsylabs.example.spring.model"]  
    19.    jpaProperties=[  
    20.     "hibernate.format_sql":"true",  
    21.     "hibernate.show_sql":"true",  
    22.     "hibernate.hbm2ddl.auto":"create"  
    23.     ]  
    24.   }  
    25.     
    26.   transactionManager(JpaTransactionManager){  
    27.    entityManagerFactory=entityManagerFactory  
    28.   }  
    29.     
    30.   conferenceDao(JpaConferenceDaoImpl){  
    31.   }  
    32.   
    33.  }  
  • 相关阅读:
    11C++11通用为本,专用为末_2
    10C++11通用为本,专用为末_1
    09C++11保证稳定性和兼容性
    21变量名的力量_2
    NOIP2018 游记
    CF767C 经典的树形DP
    CF1A Theatre Square
    洛谷P1720 月落乌啼算钱
    洛谷P3388 缩点
    NOIP2017D2T1 奶酪 洛谷P3958
  • 原文地址:https://www.cnblogs.com/duanxz/p/7491201.html
Copyright © 2011-2022 走看看