编程式事务和注解事务使用

1:


@Component
public class SpringContextUtil implements ApplicationContextAware {
    public static ApplicationContext applicationContext = null;

    public SpringContextUtil() {
    }

    @SuppressWarnings("unchecked")
	public static  T getBean(String beanName, Class beanType) {
        Assert.isTrue(applicationContext != null, "应用上下文不能为空");
        Object bean = applicationContext.getBean(beanName);
        return bean == null ? null : (T)bean;
    }

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

public class TransactionUtil {
	
	/**
	 * 获取事务模板
	 * 
	 * @return
	 */
	public static TransactionTemplate getTransactionTemplate(){
		return SpringContextUtil.getBean("transactionTemplate", TransactionTemplate.class);
	}


	


}
 return TransactionUtil.getTransactionTemplate().execute(new TransactionCallback() {
            @Override
            public XXXXXdoInTransaction(TransactionStatus status) {
                事务代码
            }
        });

		
	
	
	


	
	
		
			
		
	

 

你可能感兴趣的:(Java,事务,编程)