- 浏览: 101256 次
- 性别:
- 来自: 杭州
最新评论
让Spring架构减化事务配置
Spring颠覆了以前的编程模式,引入了IOC等全新的概念,广受大家的喜爱。目前大多数j2ee项目都已经采用Spring框架。Spring最大的问题是太多的配置文件,使得你不仅需要维护程序代码,还需要额外去维护相关的配置文件。最典型的就是事务配置(注:这里的“事务配置”都指“声明式事务配置”),在Spring中进行事务配置除了定义对象自身的bean外,还需要定义一个进行事务代理的bean.如果你有n个类需要引入事务,那么你就必须定义2n个bean。维护这些bean的代价是十分昂贵的,所以必须要对事务配置进行减化。如果你是基于Spring进行架构设计,那么作为一个好的架构设计师,应该把一些公共的方面进行简化,让项目的开发人员只关心项目的业务逻辑,而不要花费太多的精力去关心业务逻辑之外的太多东西。所以作为一个好的架构就应该把事务管理进行简化,让程序员花在编程之外的工作最小化。
1. Spring声明式事务配置的几种方法
在Spring中进行事务控制首先要选择适当的事务管理器,其次为程序选择划分事务的策略。如果只有单个事务性资源,可以从“单一资源”的PlatformTransactionManger实现当中选择一个,这些实现有:DataSourceTransactionManager,HibernateTransactionManager, JdoTransactionManager,PersistenceBrokerTransactionManager和JmsTransactionManager。根据你所采用的数据库持久化技术选择。如果你的项目运行于支持JTA的服务器,那么将选择JtaTransactionManger,将会支持多资源事务。
下表将为你选择适当的事务管理器提供参考。
技术 事务管理器 内建的事务支持
JDBC DataSurceTransactionManagerJtaTransactionManager JdbcTemplate和org.springframework.jdbc.object包中的所有类
IBATIS DataSourceTransactionManagerJtaTransactionManager SqlMapClientTemplate和SqlClientTemplate
Hibernate HibernateTransactionManagerJtaTransactionManager HibernateTemplate和HibernateInterceptor
JDO JdoTransactionManagerJtaTransactionManager JdoTemplate和JdoInterceptor
ApacheOJB PersistenceBrokerTransactionManagerJtaTransactionManager PersistenceBrokerTemplate
JMS JmsTransactionManager JmsTemplate
在划分事务时,我们需要进行事务定义,也就是配置事务的属性。事务的属性有传播行业,隔离级别,超时值及只读标志。TransactionAttribute接口指定哪些异常将导致一个回滚,哪些应该一次性提交。
(1) 使用ProxyFactoryBean 和TransactionInterceptor
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
transactionInterceptor
通过ProxyFacgtoryBean和TransactionInterceptor组合使用,可以对事务进行更多的控制。所有需要事务控制的对象可以共享一个transactionInterceptor的事务属性。
(2) 使用TransactionProxyFactoryBean
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
abstract="true">
ref="com.prs.application.ehld.sample.biz.service.sampleService.target" />
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
使用TransactionProxyFactoryBean需要为每一个代理对象都去定义自己的事务属性。
(3) 使用TransactionProxyFactoryBean及abstract属性来简化配置
这种方工也是目前使用得最多的一种声明式事务配置方法
abstract="true">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
使用abstract属性,可以让代理对象可以共享一个定义好的事务属性,使配置简化。
(4)使用BeanNameAutoProxyCreator
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
transactionInterceptor
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
使用BeanNameAutoProxyCreator可以由框架来提供适当的代理,由一个transactionInterceptor统一定义事务属性,只需要把需要事务控制的bean加到beannames的列表。
对于需要大量声明式事务的bean,BeanNameAutoProxyCreator是十分方便的。减少了代理bean的定义,还可以灵活的决定一个bean是否进行事务控制。
上面四种方法是在Spring中常见的声明式事务配置方法,其中使用TransactionProxyFactoryBean及abstract属性进行配置是最常见的简化方法。
我们暂且把需要进行事务控制的bean叫事务bean.把依赖和调用它的bean叫做业务bean。对事务bean进行代理叫做事务代理bean.
1. 使用ProxyFactoryBean 和TransactionInterceptor,可以由一个TransactionInterceptor统一定义事务属性,对于每一个事务bean都需要再定义一个事务代理bean。如果有n个事务bean,那么就需要定义和维护2n个bean。并且注入到业务bean的不是事务bean本身,而是要求用事务代理bean注入。这增加了理解的难度。
2. 使用TransactionProxyFactoryBean需要为每一个事务代理bean都定义自己的事务属性,除了需要维护2n个bean外,还需要为每一个事务代理bean定义事务属性。可以说是最麻烦的。同样需要把事务代理bean注入到业务bean,增加了理解的难度和项目的复杂度。
3. 使用TransactionProxyFactoryBean及abstract属性是对使用TransactionProxyFactoryBean的一种简单化配置,可以让所有的事务bean共享一致的事务属性定义。需要维护2n个bean,需要把事务代理bean注入到业务bean。
4. 使用BeanNameAutoProxyCreator最适合在框架中使用,只需要维护n个bean。也无需要事务代理bean。直接把事务bean注入业务bean中。但是它必须把需要事务控制的bean加到beanNames列表中。
2.类型自动代理创建器BeanClassTypeAutoProxyCreator
得于BeanNameAutoProxyCreator的启示,BeanNameAutoProxyCreator可以实现框架来实现自动代理。它只是把需要代理的bean加入beanNames属性列表。大大的简化了代理的配置,减少了代理bean的定义,使用事务bean注入业务对象,而不是代理bean注入,更合乎事务逻辑。BeanNameAutoProxyCreator仍然需要开发人员除了定义业务bean外,还需要关心事务的定义,当然已经简单了很多。如果能实现一个BeanClassTypeAutoProxyCreator,为它指定一个可以代理的ClassType列表,那么在context中所有属于ClassType和其子类的bean都自动获得代理。
实现思路:
1.BeanNameAutoProxyCreator继承了AbstractAutoProxyCreator,去实现方法:
protected abstract Object[] getAdvicesAndAdvisorsForBean(
Class beanClass, String beanName, TargetSource customTargetSource)
在BeanNameAutoProxyCreator中的实现是判断beanName 是存在于beanNames列表,如果能找到则Object[]不对空。否则返回null。
所以BeanClassTypeAutoProxyCreator也应该继承AbstractAutoProxyCreator。
getAdvicesAndAdvisorsForBean方法的实现可以参照BeanNameAutoProxyCreator方法的实现
2.BeanClassTypeAutoProxyCreator需要有一个进行代理的ClassType列表,在bean进行初始化后就在context中查找类型为ClassType列表中类型的所有beanName.从而获得一个beanNames列表。
获得beanNames列表后就可以像BeanNameAutoProxyCreator一样实现自动代理了。
3.要想获得当前context,我们可以实现ApplicationContextAware接口。让BeanClassTypeAutoProxyCreator的bean可以获得当前context.
4. 要在bean进行初始化动作,可以实现InitializingBean接口,实现afterPropertiesSet,在这个方法中在context中根据classType查找获得相关的beanName的列表。
5. 写一个空接口,里面没有任何方法。需要事务代理的类实现这个空接口。
这样,只需要把这个空接口的全类名作为BeanClassTypeAutoProxyCreator的classTypes参数值,然后所有需要代理的类都去实现这个接口就可以自动获得代理了。无再需要任何配置。这样就可以让程序员专心于业务逻辑的开发,而无需要去关心事务控制方法,就像是没有使用事务一样。
完整的实现类如下:
BeanClassTypeAutoProxyCreator.java
/**
* 根据类型自动代理Creator
*
* @author yuanguangdong date: Jul 13, 2004
*/
public class BeanClassTypeAutoProxyCreator extends AbstractAutoProxyCreator
implements ApplicationContextAware, InitializingBean {
/** Logger that is available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
/** ApplicationContext this object runs in */
private ApplicationContext applicationContext;
/** MessageSourceAccessor for easy message access */
private MessageSourceAccessor messageSourceAccessor;
/**被代理的bean别名列表**/
private List beanNames;
/**被代理的classType列表**/
private List classTypes;
//---------------------------------------------------------
//实现AbstractAutoProxyCreator的抽像方法
//---------------------------------------------------------
/**
* @see org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getAdvicesAndAdvisorsForBean(java.lang.Class,
* java.lang.String, org.springframework.aop.TargetSource)
*/
protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass,
String beanName, TargetSource targetSource) throws BeansException {
if (this.beanNames != null) {
if (this.beanNames.contains(beanName)) {
return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
}
}
return DO_NOT_PROXY;
}
//-------------------------------------------------------
//实现ApplicationContextAware接口方法
//-------------------------------------------------------
/**
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
public void setApplicationContext(ApplicationContext context)
throws BeansException {
if (context == null && !isContextRequired()) {
// Reset internal context state.
this.applicationContext = null;
this.messageSourceAccessor = null;
} else if (this.applicationContext == null) {
// Initialize with passed-in context.
if (!requiredContextClass().isInstance(context)) {
throw new ApplicationContextException(
"Invalid application context: needs to be of type ["
+ requiredContextClass().getName() + "]");
}
this.applicationContext = context;
this.messageSourceAccessor = new MessageSourceAccessor(context);
initApplicationContext();
} else {
// Ignore reinitialization if same context passed in.
if (this.applicationContext != context) {
throw new ApplicationContextException(
"Cannot reinitialize with different application context: current one is ["
+ this.applicationContext
+ "], passed-in one is [" + context + "]");
}
}
}
/**
* Determine whether this application object needs to run in an
* ApplicationContext.
*
* Default is "false". Can be overridden to enforce running in a context
* (i.e. to throw IllegalStateException on accessors if outside a context).
*
* @see #getApplicationContext
* @see #getMessageSourceAccessor
*/
protected boolean isContextRequired() {
return true;
}
/**
* Determine the context class that any context passed to
* setApplicationContext must be an instance of. Can be
* overridden in subclasses.
*
* @see #setApplicationContext
*/
protected Class requiredContextClass() {
return ApplicationContext.class;
}
/**
* Return the ApplicationContext instance used by this object.
*/
public final ApplicationContext getApplicationContext()
throws IllegalStateException {
if (this.applicationContext == null && isContextRequired()) {
throw new IllegalStateException(
"ApplicationObjectSupport instance [" + this
+ "] does not run in an ApplicationContext");
}
return applicationContext;
}
/**
* Return a MessageSourceAccessor for the application context used by this
* object, for easy message access.
*
* @throws IllegalStateException
* if not running in an ApplicationContext
*/
protected final MessageSourceAccessor getMessageSourceAccessor()
throws IllegalStateException {
if (this.messageSourceAccessor == null && isContextRequired()) {
throw new IllegalStateException(
"ApplicationObjectSupport instance [" + this
+ "] does not run in an ApplicationContext");
}
return this.messageSourceAccessor;
}
public void setClassTypes(String[] classTypes) {
this.classTypes = Arrays.asList(classTypes);
}
/**
* Subclasses can override this for custom initialization behavior. Gets
* called by setApplicationContext after setting the context
* instance.
*
* Note: Does not get called on reinitialization of the context but
* rather just on first initialization of this object's context reference.
*
* @throws ApplicationContextException
* in case of initialization errors
* @throws BeansException
* if thrown by ApplicationContext methods
* @see #setApplicationContext
*/
protected void initApplicationContext() throws BeansException {
}
//-----------------------------------
//实现InitializingBean接口方法
//-----------------------------------
/**
* 查找指定classType的beanName列表
*/
private List getBeanNames(String classType) {
List beanNameList = null;
try {
String[] beanName = this.getApplicationContext()
.getBeanNamesForType(Class.forName(classType), true, false);
if (beanName != null) {
beanNameList = Arrays.asList(beanName);
}
} catch (ClassNotFoundException ex) {
throw new IllegalArgumentException("Class not found: "
+ ex.getMessage());
}
return beanNameList;
}
/**
*
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
if (classTypes != null && !classTypes.isEmpty()) {
beanNames = new ArrayList();
for (int i = 0; i < classTypes.size(); i++) {
String classType = (String) classTypes.get(i);
List aList = getBeanNames(classType);
beanNames.addAll(aList);
}
}
if (logger.isDebugEnabled()) {
for (int i = 0; i < beanNames.size(); i++) {
logger.debug("printBean:" + (String) beanNames.get(i));
}
}
}
}
3.使用BeanClassTypeAutoProxyCreator
3.1为了使用BeanClassTypeAutoProxyCreator,将为所有需要进行代理的类定一个接口。
package com.prs.application.ehld.biz.service;
public interface BaseService {
}
3.2 让需要代理的类实现或继承这个公共接口
package com.prs.application.ehld.sample.biz.service;
public interface SampleService extends BaseService {
public void setUserInfoDAO(UserInfoDAO userInfoDAO);
public void insertUserInfo(UserInfoDTO userInfo) throws BusinessServiceException;
}
3.3 配置事务代理
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="com.prs.application.ehld.common.aotoproxy.BeanClassTypeAutoProxyCreator">
transactionInterceptor
com.prs.application.ehld.biz.service.BaseService
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
效果:只需要定义BeanClassTypeAutoProxyCreator,把需要代理的类型BaseService作为classTypes的值。这样任何实现了BaseService接口的类都自动获得代理。使得程序员就像配置普通bean一样去配置一个需要事务代理的bean。使得程序员只需要去关心业务逻辑。而无需要去关注事务这些框架应该支持的事情。特别是当开发团队成员水平不一,或团队人员流动性大时,BeanClassTypeAutoProxyCreator就发挥了它的作用。一个好的架构设计应该对事务控制,异常处理,日志记录这些方面进行统一的规划和处理,才能保证系统的健壮性。
采用Spring框架进行项目开发,我们在获得它的IOC等好处,同时给我们增加了维护太多配置文件的负担。应该尽量减少bean的定义,更多采用嵌套bean定义。否则将加大项目后期的维护成本。作为一个架构设计者更是应该把通用性比较强的方面进行统一规划。
Spring颠覆了以前的编程模式,引入了IOC等全新的概念,广受大家的喜爱。目前大多数j2ee项目都已经采用Spring框架。Spring最大的问题是太多的配置文件,使得你不仅需要维护程序代码,还需要额外去维护相关的配置文件。最典型的就是事务配置(注:这里的“事务配置”都指“声明式事务配置”),在Spring中进行事务配置除了定义对象自身的bean外,还需要定义一个进行事务代理的bean.如果你有n个类需要引入事务,那么你就必须定义2n个bean。维护这些bean的代价是十分昂贵的,所以必须要对事务配置进行减化。如果你是基于Spring进行架构设计,那么作为一个好的架构设计师,应该把一些公共的方面进行简化,让项目的开发人员只关心项目的业务逻辑,而不要花费太多的精力去关心业务逻辑之外的太多东西。所以作为一个好的架构就应该把事务管理进行简化,让程序员花在编程之外的工作最小化。
1. Spring声明式事务配置的几种方法
在Spring中进行事务控制首先要选择适当的事务管理器,其次为程序选择划分事务的策略。如果只有单个事务性资源,可以从“单一资源”的PlatformTransactionManger实现当中选择一个,这些实现有:DataSourceTransactionManager,HibernateTransactionManager, JdoTransactionManager,PersistenceBrokerTransactionManager和JmsTransactionManager。根据你所采用的数据库持久化技术选择。如果你的项目运行于支持JTA的服务器,那么将选择JtaTransactionManger,将会支持多资源事务。
下表将为你选择适当的事务管理器提供参考。
技术 事务管理器 内建的事务支持
JDBC DataSurceTransactionManagerJtaTransactionManager JdbcTemplate和org.springframework.jdbc.object包中的所有类
IBATIS DataSourceTransactionManagerJtaTransactionManager SqlMapClientTemplate和SqlClientTemplate
Hibernate HibernateTransactionManagerJtaTransactionManager HibernateTemplate和HibernateInterceptor
JDO JdoTransactionManagerJtaTransactionManager JdoTemplate和JdoInterceptor
ApacheOJB PersistenceBrokerTransactionManagerJtaTransactionManager PersistenceBrokerTemplate
JMS JmsTransactionManager JmsTemplate
在划分事务时,我们需要进行事务定义,也就是配置事务的属性。事务的属性有传播行业,隔离级别,超时值及只读标志。TransactionAttribute接口指定哪些异常将导致一个回滚,哪些应该一次性提交。
(1) 使用ProxyFactoryBean 和TransactionInterceptor
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
transactionInterceptor
通过ProxyFacgtoryBean和TransactionInterceptor组合使用,可以对事务进行更多的控制。所有需要事务控制的对象可以共享一个transactionInterceptor的事务属性。
(2) 使用TransactionProxyFactoryBean
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
abstract="true">
ref="com.prs.application.ehld.sample.biz.service.sampleService.target" />
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
使用TransactionProxyFactoryBean需要为每一个代理对象都去定义自己的事务属性。
(3) 使用TransactionProxyFactoryBean及abstract属性来简化配置
这种方工也是目前使用得最多的一种声明式事务配置方法
abstract="true">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
使用abstract属性,可以让代理对象可以共享一个定义好的事务属性,使配置简化。
(4)使用BeanNameAutoProxyCreator
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
transactionInterceptor
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
使用BeanNameAutoProxyCreator可以由框架来提供适当的代理,由一个transactionInterceptor统一定义事务属性,只需要把需要事务控制的bean加到beannames的列表。
对于需要大量声明式事务的bean,BeanNameAutoProxyCreator是十分方便的。减少了代理bean的定义,还可以灵活的决定一个bean是否进行事务控制。
上面四种方法是在Spring中常见的声明式事务配置方法,其中使用TransactionProxyFactoryBean及abstract属性进行配置是最常见的简化方法。
我们暂且把需要进行事务控制的bean叫事务bean.把依赖和调用它的bean叫做业务bean。对事务bean进行代理叫做事务代理bean.
1. 使用ProxyFactoryBean 和TransactionInterceptor,可以由一个TransactionInterceptor统一定义事务属性,对于每一个事务bean都需要再定义一个事务代理bean。如果有n个事务bean,那么就需要定义和维护2n个bean。并且注入到业务bean的不是事务bean本身,而是要求用事务代理bean注入。这增加了理解的难度。
2. 使用TransactionProxyFactoryBean需要为每一个事务代理bean都定义自己的事务属性,除了需要维护2n个bean外,还需要为每一个事务代理bean定义事务属性。可以说是最麻烦的。同样需要把事务代理bean注入到业务bean,增加了理解的难度和项目的复杂度。
3. 使用TransactionProxyFactoryBean及abstract属性是对使用TransactionProxyFactoryBean的一种简单化配置,可以让所有的事务bean共享一致的事务属性定义。需要维护2n个bean,需要把事务代理bean注入到业务bean。
4. 使用BeanNameAutoProxyCreator最适合在框架中使用,只需要维护n个bean。也无需要事务代理bean。直接把事务bean注入业务bean中。但是它必须把需要事务控制的bean加到beanNames列表中。
2.类型自动代理创建器BeanClassTypeAutoProxyCreator
得于BeanNameAutoProxyCreator的启示,BeanNameAutoProxyCreator可以实现框架来实现自动代理。它只是把需要代理的bean加入beanNames属性列表。大大的简化了代理的配置,减少了代理bean的定义,使用事务bean注入业务对象,而不是代理bean注入,更合乎事务逻辑。BeanNameAutoProxyCreator仍然需要开发人员除了定义业务bean外,还需要关心事务的定义,当然已经简单了很多。如果能实现一个BeanClassTypeAutoProxyCreator,为它指定一个可以代理的ClassType列表,那么在context中所有属于ClassType和其子类的bean都自动获得代理。
实现思路:
1.BeanNameAutoProxyCreator继承了AbstractAutoProxyCreator,去实现方法:
protected abstract Object[] getAdvicesAndAdvisorsForBean(
Class beanClass, String beanName, TargetSource customTargetSource)
在BeanNameAutoProxyCreator中的实现是判断beanName 是存在于beanNames列表,如果能找到则Object[]不对空。否则返回null。
所以BeanClassTypeAutoProxyCreator也应该继承AbstractAutoProxyCreator。
getAdvicesAndAdvisorsForBean方法的实现可以参照BeanNameAutoProxyCreator方法的实现
2.BeanClassTypeAutoProxyCreator需要有一个进行代理的ClassType列表,在bean进行初始化后就在context中查找类型为ClassType列表中类型的所有beanName.从而获得一个beanNames列表。
获得beanNames列表后就可以像BeanNameAutoProxyCreator一样实现自动代理了。
3.要想获得当前context,我们可以实现ApplicationContextAware接口。让BeanClassTypeAutoProxyCreator的bean可以获得当前context.
4. 要在bean进行初始化动作,可以实现InitializingBean接口,实现afterPropertiesSet,在这个方法中在context中根据classType查找获得相关的beanName的列表。
5. 写一个空接口,里面没有任何方法。需要事务代理的类实现这个空接口。
这样,只需要把这个空接口的全类名作为BeanClassTypeAutoProxyCreator的classTypes参数值,然后所有需要代理的类都去实现这个接口就可以自动获得代理了。无再需要任何配置。这样就可以让程序员专心于业务逻辑的开发,而无需要去关心事务控制方法,就像是没有使用事务一样。
完整的实现类如下:
BeanClassTypeAutoProxyCreator.java
/**
* 根据类型自动代理Creator
*
* @author yuanguangdong date: Jul 13, 2004
*/
public class BeanClassTypeAutoProxyCreator extends AbstractAutoProxyCreator
implements ApplicationContextAware, InitializingBean {
/** Logger that is available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
/** ApplicationContext this object runs in */
private ApplicationContext applicationContext;
/** MessageSourceAccessor for easy message access */
private MessageSourceAccessor messageSourceAccessor;
/**被代理的bean别名列表**/
private List beanNames;
/**被代理的classType列表**/
private List classTypes;
//---------------------------------------------------------
//实现AbstractAutoProxyCreator的抽像方法
//---------------------------------------------------------
/**
* @see org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getAdvicesAndAdvisorsForBean(java.lang.Class,
* java.lang.String, org.springframework.aop.TargetSource)
*/
protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass,
String beanName, TargetSource targetSource) throws BeansException {
if (this.beanNames != null) {
if (this.beanNames.contains(beanName)) {
return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
}
}
return DO_NOT_PROXY;
}
//-------------------------------------------------------
//实现ApplicationContextAware接口方法
//-------------------------------------------------------
/**
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
public void setApplicationContext(ApplicationContext context)
throws BeansException {
if (context == null && !isContextRequired()) {
// Reset internal context state.
this.applicationContext = null;
this.messageSourceAccessor = null;
} else if (this.applicationContext == null) {
// Initialize with passed-in context.
if (!requiredContextClass().isInstance(context)) {
throw new ApplicationContextException(
"Invalid application context: needs to be of type ["
+ requiredContextClass().getName() + "]");
}
this.applicationContext = context;
this.messageSourceAccessor = new MessageSourceAccessor(context);
initApplicationContext();
} else {
// Ignore reinitialization if same context passed in.
if (this.applicationContext != context) {
throw new ApplicationContextException(
"Cannot reinitialize with different application context: current one is ["
+ this.applicationContext
+ "], passed-in one is [" + context + "]");
}
}
}
/**
* Determine whether this application object needs to run in an
* ApplicationContext.
*
* Default is "false". Can be overridden to enforce running in a context
* (i.e. to throw IllegalStateException on accessors if outside a context).
*
* @see #getApplicationContext
* @see #getMessageSourceAccessor
*/
protected boolean isContextRequired() {
return true;
}
/**
* Determine the context class that any context passed to
* setApplicationContext must be an instance of. Can be
* overridden in subclasses.
*
* @see #setApplicationContext
*/
protected Class requiredContextClass() {
return ApplicationContext.class;
}
/**
* Return the ApplicationContext instance used by this object.
*/
public final ApplicationContext getApplicationContext()
throws IllegalStateException {
if (this.applicationContext == null && isContextRequired()) {
throw new IllegalStateException(
"ApplicationObjectSupport instance [" + this
+ "] does not run in an ApplicationContext");
}
return applicationContext;
}
/**
* Return a MessageSourceAccessor for the application context used by this
* object, for easy message access.
*
* @throws IllegalStateException
* if not running in an ApplicationContext
*/
protected final MessageSourceAccessor getMessageSourceAccessor()
throws IllegalStateException {
if (this.messageSourceAccessor == null && isContextRequired()) {
throw new IllegalStateException(
"ApplicationObjectSupport instance [" + this
+ "] does not run in an ApplicationContext");
}
return this.messageSourceAccessor;
}
public void setClassTypes(String[] classTypes) {
this.classTypes = Arrays.asList(classTypes);
}
/**
* Subclasses can override this for custom initialization behavior. Gets
* called by setApplicationContext after setting the context
* instance.
*
* Note: Does not get called on reinitialization of the context but
* rather just on first initialization of this object's context reference.
*
* @throws ApplicationContextException
* in case of initialization errors
* @throws BeansException
* if thrown by ApplicationContext methods
* @see #setApplicationContext
*/
protected void initApplicationContext() throws BeansException {
}
//-----------------------------------
//实现InitializingBean接口方法
//-----------------------------------
/**
* 查找指定classType的beanName列表
*/
private List getBeanNames(String classType) {
List beanNameList = null;
try {
String[] beanName = this.getApplicationContext()
.getBeanNamesForType(Class.forName(classType), true, false);
if (beanName != null) {
beanNameList = Arrays.asList(beanName);
}
} catch (ClassNotFoundException ex) {
throw new IllegalArgumentException("Class not found: "
+ ex.getMessage());
}
return beanNameList;
}
/**
*
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
if (classTypes != null && !classTypes.isEmpty()) {
beanNames = new ArrayList();
for (int i = 0; i < classTypes.size(); i++) {
String classType = (String) classTypes.get(i);
List aList = getBeanNames(classType);
beanNames.addAll(aList);
}
}
if (logger.isDebugEnabled()) {
for (int i = 0; i < beanNames.size(); i++) {
logger.debug("printBean:" + (String) beanNames.get(i));
}
}
}
}
3.使用BeanClassTypeAutoProxyCreator
3.1为了使用BeanClassTypeAutoProxyCreator,将为所有需要进行代理的类定一个接口。
package com.prs.application.ehld.biz.service;
public interface BaseService {
}
3.2 让需要代理的类实现或继承这个公共接口
package com.prs.application.ehld.sample.biz.service;
public interface SampleService extends BaseService {
public void setUserInfoDAO(UserInfoDAO userInfoDAO);
public void insertUserInfo(UserInfoDTO userInfo) throws BusinessServiceException;
}
3.3 配置事务代理
class="org.springframework.transaction.interceptor.TransactionInterceptor">
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_REQUIRED
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
PROPAGATION_SUPPORTS,readOnly
class="com.prs.application.ehld.common.aotoproxy.BeanClassTypeAutoProxyCreator">
transactionInterceptor
com.prs.application.ehld.biz.service.BaseService
class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
效果:只需要定义BeanClassTypeAutoProxyCreator,把需要代理的类型BaseService作为classTypes的值。这样任何实现了BaseService接口的类都自动获得代理。使得程序员就像配置普通bean一样去配置一个需要事务代理的bean。使得程序员只需要去关心业务逻辑。而无需要去关注事务这些框架应该支持的事情。特别是当开发团队成员水平不一,或团队人员流动性大时,BeanClassTypeAutoProxyCreator就发挥了它的作用。一个好的架构设计应该对事务控制,异常处理,日志记录这些方面进行统一的规划和处理,才能保证系统的健壮性。
采用Spring框架进行项目开发,我们在获得它的IOC等好处,同时给我们增加了维护太多配置文件的负担。应该尽量减少bean的定义,更多采用嵌套bean定义。否则将加大项目后期的维护成本。作为一个架构设计者更是应该把通用性比较强的方面进行统一规划。
发表评论
-
使用jstack分析cpu消耗过高的问题
2013-04-12 16:58 688http://www.iteye.com/topic/1114 ... -
jvm调试命令
2013-03-29 16:43 805http://hhhk.iteye.com/blog/1746 ... -
名词解释
2013-03-21 16:04 623ABA问题:http://www.iteye.com/prob ... -
nio reactor proactor(转)
2013-03-21 14:15 659两种I/O多路复用模式:Reactor和Proactor 一般 ... -
同步io,异步io,阻塞io,非阻塞io
2013-03-21 13:59 836按照《Unix网络编程》的划分,IO模型可以分为:阻塞IO、非 ... -
清华大学 老师 一席话 [转]
2011-02-18 18:04 6651、 一个人,如果你不 ... -
关于研发管理杂想
2010-07-27 23:32 769最近做开发的反思,感觉软件开发流程很随意,做一个东西不经过 ... -
java中synchronized用法
2010-05-28 16:33 919synchronized的一个简单例 ... -
转:class cannot be resolved to a type 或者JSP import class cannot be resolved to
2010-02-05 12:21 16828错误调试解析:class cannot be resolved ... -
一个小问题
2010-01-06 21:17 675不用jquery时很简单如下: var id="in ... -
synchronized 锁机制的经典解释
2009-09-28 15:50 836打个比方:一个object就像一个大房子,大门永远打开。房子里 ... -
虚拟主机为何不能直接用IP访问
2009-08-19 21:42 1683虚拟主机,顾名思义,是虚拟的主机,也就是说在一台服务器可以虚拟 ... -
动态代理推荐文章
2008-11-14 16:54 693http://blog.csdn.net/tyrone1979 ... -
Spring 的优秀工具类盘点
2008-11-06 16:13 751http://www.ibm.com/developerwor ... -
追MM与Java的23种设计模式
2008-10-07 14:29 758创建型模式 1 ... -
关于spring的 单实例问题
2008-05-30 13:44 1721以前一直没弄明白spring的bean是单实例的,在并发的情况 ... -
cas单点登陆配置心得--原创
2008-05-29 15:46 1772下午花了几个小时终于把yale的cas给搞定了,了却了一个 ... -
介绍一篇关于session的好文章,写的很详细--转
2008-05-28 09:32 1236目录: 一、术语session ... -
iBatis的JpetStore示例中MVC机制实现的研究--转
2008-04-19 23:12 1181JpetStore中的Action与普通Struts的Acti ... -
hibernate 需要导入的包--转
2008-02-21 16:15 2119hibernate2.jar: Hibernate的 ...
相关推荐
12012_RocketMQ_Producer_事务消息机制详解38:15 13013_RocketMQ_Consumer_Push和Pull模式及使用详解28:21 14014_RocketMQ_Consumer_配置参数详解06:45 15015_RocketMQ_Consumer_重试策略详解33:08 16016_RocketMQ_...
- chapter1:[基本项目构建(可作为工程脚手架),引入...由于Spring Cloud偏宏观架构,Spring Boot偏微观细节,内容上越来越多,为了两部分内容不互相干扰,所以迁移Spring Cloud内容到:[SpringCloud-Learning项目]...
发布于2013-5-6项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring2.0AOP类库即可。添加方法:点击项目右键->BuildPath->Addlibrarys:打开AddLibraries对话框,然后选定...
JAVA-ACE-架构师系列视频教程RocketMQ订单实战上目录介绍: 1001_RocketMQ_简介 2002_RocketMQ_核心概念详解 3003_RocketMQ_集群构建模型详解(一) 4004_RocketMQ_集群构建模型详解(二) 5005_RocketMQ_双主模式...
精通spring 源代码 对JavaEE5及Spring2.5进行了综述。包括Java EE5,步入Spring2.5,获得Spring2.5发布版和源码。启动Spring2.5使能项目 详细介绍Spring2.5核心技术。包括控制反转容器,面向切面编程 对DA0层...
1、微内核2、配置简单3、模块化4、开箱即用5、完全兼容Spring6、设计理念极其先进,很多思想来自OSGi,但是在现有技术的实现 缺点: 二次改造定制难缺少成熟的SOA或者RPC框架Dubbox: 1、完全兼容...
后端采用 Spring Cloud Alibaba 微服务架构,注册中心 + 配置中心 Nacos,消息队列 RocketMQ,定时任务 XXL-Job,服务保障 Sentinel,服务网关 Gateway,分布式事务 Seata 数据库可使用 MySQL、Oracle、PostgreSQL...
高并发,分布式架构 尚硅谷谷粒商城实现 微服务 后台管理系统 网关服务 商品系统 全文检索系统 订单系统 购物车系统 仓库系统 秒杀系统 物流系统 会员系统 优惠系统 支付系统 技术栈 反向代理、动静分离 - Nginx ...
SpringColud1简易分布式 ● cloud-config-server:配置服务器-(通过git获取配置) ● cloud-eureka-server:eureka注册服务器 ● cloud-simple-service:一个使用mybatis的数据库应用,服务端 ● cloud-simple-...
本资源是一个S2SH架构整合的一个完整例子,包含:struts2的完整配置及实例,使用c3p0连接池,Spring的事务管理、类路径扫描管理功能、注解功能。例子本人已测试通过,所有配置和代码都有完整的注释,适合初学者或者...
Java SSM项目是一种使用Java语言和SSM框架(Spring + Spring MVC + MyBatis)开发的Web应用程序。SSM是一种常用的Java开发框架组合,它结合了Spring框架、Spring MVC框架和MyBatis框架的优点,能够快速构建可靠、...
2022.0.4、Spring Cloud Alibaba 2023.0.0.0、Nacos 2.3.1 等主流技术栈开发的多租户系统,遵循SpringBoot 编程思想,高度模块化和可配置化。具备服务发现、配置、熔断、限流、降级、监控、多级缓存、分布式事务、...
采用前后端分离的模式,微服务版本前端vue 后端采用Spring Boot、Spring Cloud & Alibaba。注册中心、配置中心选型Nacos,权限认证使用Redis。流量控制框架选型Sentinel,分布式事务选型Seata。 商品管理:商品类型...
远程配置:默认使用携程的Apollo作为远程配置服务,个人觉得比Nacos的配置好用。 分布式基础公共支持:robert-core包含了常用的公共核心类和常见的中间件集成包,开箱即用。 网关服务:以Spring Cloud官方的spring-...
阿里巴巴的微服务化架构还使用了多种技术手段,包括服务注册、服务发现、配置中心、API 网关、消息队列等。这些技术手段使得微服务之间的通信变得更加高效和可靠。 微服务化架构的优势在于,它可以使得系统的维护和...
Spring 事务注解 业务特性: 业务代码生成器(支持自动生成 Service、Controller、数据模型代码) Spring Session Redis 分布式登录 全局请求响应拦截器(记录日志) 全局异常处理器 自定义错误码 封装通用响应类 ...
abapi-spring-cloud-alibaba简介本项目为spring-cloud-alibaba整合分为框架整合和常用插件模块abapi-cloud-start是spring cloud alibaba 微服务架构组件模块abapi-cloud-web-start是springboot启动框架,引入改jar包...
基于Spring Cloud+Vue的班级事务管理系统源码(毕设源码)+项目使用说明.zip 【系统架构】 项目采用B/S架构,前后端通讯采用RESTful API,数据格式使用Json,认证Token格式采用JWT。 身份认证使用Spring Security ...
3. 掌握整合spring和hibernate的持久化操作编程 4.掌握基于AOP的声明式事务编程 按照三层架构构建web项目,在业务层添加事务控制。 1.创建web project项目命名为aopweb 2.添加spring支持、hibernate支持 3.配置WEB...
基于Spring Boot、Spring Cloud & Alibaba的分布式微服务架构权限管理系统,同时提供了 Vue3 的版本。若依是一套全部开源的快速开发平台,毫无保留给个人及企业免费使用。采用前后端分离的模式,微服务版本前端(基于...