Spring container initialization execution

When container Initialization執行業務處理的方式爲如下幾種:java

1.Listener,例如:ApplicationEventListener,當容器Initialization後會回調實現該接口的方法。spring

2.Web.xml,使用<sevlet>元素,註冊一個Servlet類,在init method中實現相應的處理,若是經過這種方式,不能直接向Servlet中注入依賴,由於該Servlet不屬於Spring容器管理,如需被Spring管理,使用@Component註解可交由Spring維護,或者經過得到ApplicationContext容器,來獲取相應依賴Bean,此時須要得到ApplicationContext容器,能夠經過實現ApplicationContextAware。數據庫

public final class SpringContextUtil implements ApplicationContextAware
{
	private static ApplicationContext ctx;

	public static <T> T getBean(Class<T> clazz )  {
		return SpringContextUtil.ctx.getBean(clazz) ;
	}
	
	public static Object getBean(String beanName) throws BeansException
	{
		return SpringContextUtil.ctx.getBean(beanName);
	}
	
	public static boolean containsBean(String beanName)
	{
		return SpringContextUtil.ctx.containsBean(beanName);
	}
	
	public static ApplicationContext getApplicationContext()
	{
		return SpringContextUtil.ctx;
	}

	public void setApplicationContext(ApplicationContext ctx) throws BeansException {
		SpringContextUtil.ctx = ctx;
	}

}

3.InitializeBean interface(Spring提供),爲實例對象添加BeanPostProcessor後置處理器,當完成Bean初始化後的回調處理。express

執行關於數據庫業務處理時發現問題,使用Listener的方式,事務不會生效。apache

applicationContext.xml編程

<!-- Configuration DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="{jdbc.driver}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  <property name="validationQuery" value="SELECT 1" />
</bean>

<!-- Configuration transactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 聲明式事務使用註解 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- Use of the inteceptor -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
    <tx:method name="*" propagation="REQUIRED" />
  </tx:attributes>
</tx:advice>

<aop:config>
  <aop:pointcut id="interceptorPoints" expression="execution(* com.matrixframework.panda.*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPoints" />
</aop:config>

兩種方式事務都沒法生效,最終採用編程式事務解決:app

applicationContext.xmlide

<!-- Configuration DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="{jdbc.driver}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  <property name="validationQuery" value="SELECT 1" />
</bean>

<!-- configuration transactionManager -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"/>
</bean>

<!-- Use the TransactionTemplate -->    
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
  <property name="transactionManager" ref="transactionManager"/>
</bean>

InitializeData.javaurl

public class InitializDataService implements InitializingBean {
	
	private static final Logger log = LoggerFactory.getLogger(InitializDataService.class);
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private TransactionTemplate transactionTemplate;
	
    @Override
	public void afterPropertiesSet() throws Exception {
		initializeData();
	}
	
	public void initializeData() {
      transactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				try {
                    //TODO do something
					...
					log.debug("initalize data successfull");
				} catch (Exception e) {
					status.setRollbackOnly();  //When an exception occurs rollback
					log.debug("initialize data error:" + e.getMessage(), e);
				}
			}
		});
    }
}
相關文章
相關標籤/搜索