- 浏览: 53357 次
- 性别:
- 来自: 上海
文章分类
最新评论
谈谈Spring 2.x中简化配置的问题
Spring 2.x在配置文件的简化的方面做了很多工作,原来1.x中比较麻烦的配置都已经拥有了比较完美的解决方案。最近刚看完《精通Spring 2.x --企业应用开发精解》的书,结合自己的经验整理一下简化配置的内容。
一、关于集合的配置
1.List
>1.x版本的
- <bean id="parentBoss" abstract="true"class="com.baobaotao.attr.Boss"> <--父<bean>
- <property name="favorites">
- <set>
- <value>看报</value>
- <value>赛车</value>
- <value>高尔夫</value>
- </set>
- </property>
- </bean>
<bean id="parentBoss" abstract="true"class="com.baobaotao.attr.Boss"> <--父<bean> <property name="favorites"> <set> <value>看报</value> <value>赛车</value> <value>高尔夫</value> </set> </property> </bean>
>2.x版本的
- <util:list id="favoriteList1" list-class="java.util.LinkedList">
- <value>看报</value>
- <value>赛车</value>
- <value>高尔夫</value>
- </util:list>
<util:list id="favoriteList1" list-class="java.util.LinkedList"> <value>看报</value> <value>赛车</value> <value>高尔夫</value> </util:list>
2.Set
> 1.x
- <bean id="boss1" class="com.baobaotao.attr.Boss">
- <property name="favorites">
- <set>
- <value>看报</value>
- <value>赛车</value>
- <value>高尔夫</value>
- </set>
- </property>
- </bean>
<bean id="boss1" class="com.baobaotao.attr.Boss"> <property name="favorites"> <set> <value>看报</value> <value>赛车</value> <value>高尔夫</value> </set> </property> </bean>
> 2.x
- <util:set id="favoriteSet1">
- <value>看报</value>
- <value>赛车</value>
- <value>高尔夫</value>
- </util:set>
<util:set id="favoriteSet1"> <value>看报</value> <value>赛车</value> <value>高尔夫</value> </util:set>
3.Map
> 1.x
- <bean id="boss1" class="com.baobaotao.attr.Boss">
- <property name="jobs">
- <map>
- <!--Map第一个元素-->
- <entry>
- <key><value>AM</value></key>
- <value>会见客户</value>
- </entry>
- <!--Map第二个元素-->
- <entry>
- <key><value>PM</value></key>
- <value>公司内部会议</value>
- </entry>
- </map>
- </property>
- </bean>
<bean id="boss1" class="com.baobaotao.attr.Boss"> <property name="jobs"> <map> <!--Map第一个元素--> <entry> <key><value>AM</value></key> <value>会见客户</value> </entry> <!--Map第二个元素--> <entry> <key><value>PM</value></key> <value>公司内部会议</value> </entry> </map> </property> </bean>
> 2.x
- <util:map id="emails1">
- <entry key="AM" value="会见客户" />
- <entry key="PM" value="公司内部会议" />
- </util:map>
<util:map id="emails1"> <entry key="AM" value="会见客户" /> <entry key="PM" value="公司内部会议" /> </util:map>
4. Properties
> 1.x
- <bean id="boss1" class="com.baobaotao.attr.Boss">
- <property name="mails">
- <props>
- <prop key="jobMail">john-office@baobaotao.com</prop>
- <prop key="lifeMail">john-life@baobaotao.com</prop>
- </props>
- </property>
- </bean>
<bean id="boss1" class="com.baobaotao.attr.Boss"> <property name="mails"> <props> <prop key="jobMail">john-office@baobaotao.com</prop> <prop key="lifeMail">john-life@baobaotao.com</prop> </props> </property> </bean>
> 2.x
<util:properties id="emailProps1" location="classpath:com/baobaotao/fb/mails.properties"/>
可以在一个属性文件中直接配置属性,这比较符合一般的项目习惯。
二、 关于事务配置
1.1.x
- <bean id="bbtForum"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <property name="transactionManager" ref="txManager" />
- <property name="target" ref="bbtForumTarget"/>
- <property name="transactionAttributes">
- <props>
- <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
<bean id="bbtForum" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="txManager" /> <property name="target" ref="bbtForumTarget"/> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean>
2.2.x
有两种新方法
a)使用@Transactional注解
在需要的服务类或服务方法处直接打上@Transactional注解,然后在Spring配置文件中启用注解事务驱动就可以了:
- @Transactional
- public class BbtForumImpl implements BbtForum {
- @Transactional(readOnly=true)
- public Forum getForum(int forumId) {
- return forumDao.getForum(forumId);
- }
- 。。。。
- }
@Transactional public class BbtForumImpl implements BbtForum { @Transactional(readOnly=true) public Forum getForum(int forumId) { return forumDao.getForum(forumId); } 。。。。 }
在Spring配置文件中相应添加上:
- <bean id="txManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager"/>
这样就OK了,简单吧:)
b)使用aop/tx
如果你的Service服务类都很规范,我觉得使用aop/tx更方面,因为不用到处打注解,在一处集中配置就OK了,可谓运筹帷幄之中,决胜于千里之外:)
- <aop:config>
- t;aop:pointcut id="serviceMethod"
- expression="execution(* com.baobaotao.service.*Forum.*(..))" />
- t;aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
- </aop:config>
- <tx:advice id="txAdvice" transaction-manager="txManager">
- <tx:attributes>
- <tx:method name="get*" read-only="false"/>
- <tx:method name="add*" rollback-for="Exception" />
- <tx:method name="update*"/>
- </tx:attributes>
- lt;/tx:advice>
<aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.baobaotao.service.*Forum.*(..))" /> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="false"/> <tx:method name="add*" rollback-for="Exception" /> <tx:method name="update*"/> </tx:attributes> </tx:advice>
三、关于AOP配置
原来1.x的AOP太麻烦了,提都不想提,直接说一下2.x的AOP。
Spring 2.x使用@AspectJ来描述切面,由于@AspectJ的语法描述能力超强,因此在Spring 2.x中使用AOP真的非常方便。
在使用@AspectJ之前,首先你得保证你所使用的JDK的版本是5.0及以上版本,否则无法使用注解技术。
Spring在处理@Aspect注解表达式时,需要使用位于spring/lib/asm下asm关联类库,将该类库的三个类包加入到类路径中:asm-2.2.2.jar、asm-commons-2.2.2.jar和asm-util-2.2.2.jar。我们在第一章中了解了asm类库的用途,它是轻量级的字节码处理框架,因为Java的反射机制无法获取入参名,Spring就利用asm处理@AspectJ中所描述的方法入参名。
此外,Spring采用AspectJ提供的@AspectJ注解类库及相应的解析类库,它位于spring/lib/aspectj目录下,将目录下的aspectjrt.jar和aspectjweaver.jar类包加入类路径中。
在做好上节中所提到的前置工作后,我们就可以开始编写一个基于@AspectJ的切面了,首先来看一个简单的例子,以便对@AspectJ有一个切身的认识。
@AspectJ采用不同的方式对AOP进行描述, 我们使用NaiveWaiter的例子来说明,这是一个希望引入切面的目标类:
- package com.baobaotao;
- public class NaiveWaiter implements Waiter {
- public void greetTo(String clientName) {
- System.out.println("NaiveWaiter:greet to "+clientName+"...");
- }
- public void serveTo(String clientName){
- System.out.println("NaiveWaiter:serving "+clientName+"...");
- }
- }
package com.baobaotao; public class NaiveWaiter implements Waiter { public void greetTo(String clientName) { System.out.println("NaiveWaiter:greet to "+clientName+"..."); } public void serveTo(String clientName){ System.out.println("NaiveWaiter:serving "+clientName+"..."); } }
下面使用@AspectJ来定义一下切面:
- package com.baobaotao.aspectj.aspectj;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- //通过该注解将PreGreetingAspect标识为一个切面
- @Aspect
- public class PreGreetingAspect{
- @Before("execution(* greetTo(..))") //<---定义切点和增强类型
- public void beforeGreeting(){ //<----增强的横切逻辑
- System.out.println("How are you");
- }
- }
package com.baobaotao.aspectj.aspectj; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; //通过该注解将PreGreetingAspect标识为一个切面 @Aspect public class PreGreetingAspect{ @Before("execution(* greetTo(..))") //<---定义切点和增强类型 public void beforeGreeting(){ //<----增强的横切逻辑 System.out.println("How are you"); } }
然后启动@AspectJ的注解切面驱动就可以了!
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- <A href="http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</A>
- http://www.springframework.org/schema/aop
- <A href="http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" target=_blank>http://www.springframework.org/schema/aop/spring-aop-2.0.xsd</A>">
- <!--基于@AspectJ切面的驱动器-->
- <aop:aspectj-autoproxy />
- <bean id="waiter" class="com.baobaotao.NaiveWaiter" />
- <bean class="com.baobaotao.aspectj.example.PreGreetingAspect" />
- </beans>
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <!--基于@AspectJ切面的驱动器--> <aop:aspectj-autoproxy /> <bean id="waiter" class="com.baobaotao.NaiveWaiter" /> <bean class="com.baobaotao.aspectj.example.PreGreetingAspect" /> </beans>
四、关于Spring 2.1添加的新功能
1.原来引入一个外面属性配置文件需要使用以下的方式:
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <!--指定属性文件地址,可以在这里定义多个属性文件-->
- <value>classpath:com/baobaotao/place/car.properties</value>
- </list>
- </property>
- <property name="fileEncoding" value="utf-8"/>
- </bean>
- <!--引用外部属性的值,对car属性进行配置-->
- <bean id="car" class="com.baobaotao.place.Car">
- <property name="brand" value="${brand}" />
- <property name="maxSpeed" value="${maxSpeed}" />
- <property name="price" value="${price}" />
- </bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!--指定属性文件地址,可以在这里定义多个属性文件--> <value>classpath:com/baobaotao/place/car.properties</value> </list> </property> <property name="fileEncoding" value="utf-8"/> </bean> <!--引用外部属性的值,对car属性进行配置--> <bean id="car" class="com.baobaotao.place.Car"> <property name="brand" value="${brand}" /> <property name="maxSpeed" value="${maxSpeed}" /> <property name="price" value="${price}" /> </bean>
使用Spring 2.1后,你只需要象下面这样配置就可以了:
<context:property-placeholder location=" classpath:com/baobaotao/place/car.properties "/>
3.注解驱动的Bean注入
大家看看这段E文就OK了,
<context:component-scan>: scans classpath using one or more "include"/"exclude" filters, and automatically registers beans
In essence, this is a third way to define beans (1: classic xml, 2:javaconfig (code), 3:<context:component-scan>, matching on annotations or types)
Default naming strategy is based on short classname of discovered bean
@Component and @Repository annotations, when used, can optionally specify a bean name to use
For filter type "annotation", the value of "expression" attribute should resolve to a Java annotation type
For filter type "assignable", the value of "expression" attribute should resolve to a Java type
For filter type "aspectj", the value of "expression" should be an "type expression" (in Pointcut language, perhaps it could be injected?)
Relevant documentation can be found in preliminary spring 2.1 manual, sections 3.10 and 3.11
In addition, this last JIRA comment for http://www.jetbrains.net/jira/browse/IDEADEV-16886#action_163502 contains two links to articles showing example usage of <context:component-scan>
<context:annotation-config> (described in 3.10 in spring 2.1 manual linked above): allows autowiring to be defined using @Resource or @Autowired annotations.
五、关于Spring 2.5添加的新功能
Spring 2.5继续对context命名空间进行了扩充,添加了好用而强大的context:load-time-weaver,可以让我们更方便地应用AspectJ。大家可以看TSS上的这篇文章,它全面讲解了Spring 2.5的新特性。
http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25
相关推荐
spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-3.2.4.RELEASE.jar spring-webmvc-...
Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...
rg.springframework.asm-3.0.1.RELEASE-A.jar
包含spring 3.0.5的所有jar文件: org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE...
org.springframework.spring-library-3.1.RELEASE.libd org.springframework.test-3.1.RELEASE.jar org.springframework.transaction-3.1.RELEASE.jar org.springframework.web.portlet-3.1.RELEASE.jar org....
org.springframework.spring-library-3.0.4.RELEASE.libd org.springframework.test-3.0.4.RELEASE.jar org.springframework.transaction-3.0.4.RELEASE.jar org.springframework.web.portlet-3.0.4.RELEASE.jar ...
Chinese translation of the Spring Framework 4.x Reference Documentation (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/) .中文翻译《Spring Framework 4.x参考文档》 — ...
spring4.x中的jar包下载,spring4.0.6下载,spring最新稳定版jar包下载 http://maven.springframework.org/release/org/springframework/spring/ 这个链接中有各种稳定版的jar包下载 目前官网上大部分都要maven下载
通过对 Spring Framework 3.2.x 源码的深入研究,我们可以更好地理解这个框架的工作原理,提高我们的设计能力和问题解决能力。这不仅有助于日常开发工作,也能为未来技术选型和架构设计提供坚实的基础。
aopalliance-1.0.jar,org.springframework.aop-3.0.0.RELEASE.jar,org.springframework.jdbc-3.0.0.RELEASEorg.springframework.beans-3.0.0.RELEASE.jar等
这两个模块的 jar 文件,`spring-data-commons-1.7.2.RELEASE.jar` 和 `spring-data-jpa-1.5.2.RELEASE.jar`,包含了它们各自的功能实现和依赖。在实际项目中,将这些 jar 文件添加到类路径,就可以利用 Spring Data...
Spring 3.x 还引入了属性抽象层,这使得开发者可以在配置文件中使用占位符(placeholder),从而减少因环境变化而频繁更改配置文件的需求。例如,可以使用 `${JAVA_HOME}/com/bank/service/${env}-config.xml"/>` 来...
spring 4.0.0 框架核心包 ...spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar
标题 "springboot2.0.x+dubbo-spring-boot-starter" 涉及的是将流行的微服务框架 Dubbo 集成到 Spring Boot 2.0.x 的项目实践中。这个集成使得开发者能够利用 Spring Boot 的便利性和 Dubbo 的高性能远程服务调用...
9. **文档资源**:压缩包中的"spring-5.2.14.RELEASE-docs.zip"可能包含了Spring Framework的官方文档,这是一份详尽的指南,涵盖了所有模块的使用方法和最佳实践,对于学习和使用Spring框架至关重要。 10. **XML与...
org.springframework.core-3.0.2.RELEASE.jar
这个文件夹包含了Spring框架所需的jar包,这些jar包涵盖了Spring的核心模块,如spring-context、spring-beans、spring-web等,以及相关的依赖库。Spring的核心模块提供了如IoC(Inversion of Control,控制反转)...
Spring 4.x 是一个重要的Java框架,专注于简化企业级应用程序开发。这个版本的Spring框架引入了许多新特性,改进和优化,以适应不断变化的开发需求和技术趋势。在本文中,我们将深入探讨Spring 4.x的核心特性、优势...
然而,有时候在导入Spring项目时,可能会遇到一些问题,例如标题中提到的"spring-cglib-repack-3.2.4.jar"和"spring-objenesis-repack-2.4.jar"这两个jar包的缺失。这些jar包对于Spring框架的正常运行至关重要,因为...
在Spring1.x中,依赖注入的概念已经引入,但支持的方式相对有限,主要是基于XML配置文件。而Spring2.x在依赖注入方面进行了扩展,增加了基于注解的依赖注入(@Autowired、@Qualifier等),使得代码更加简洁,降低了...