使用GenericApplicationContext时如何使用Spring Transaction

时间:2012-12-17 05:58:47

标签: spring spring-transactions transactional spring-annotations

使用Distributed Mysql Database时,我使用BeanDefinitionBuilder创建了多个dataSources,sessionFactory 但是当我执行Insert SQL时,事务注释似乎不起作用 使用下面的getBean('bean name')方法

((SqlSessionTemplate)context.getBean('bean name'))。insert(“xxxx”,params)

你会解释我错过了什么吗?

Private GenericApplicationContext context = new GenericApplicationContext();

BeanDefinitionBuilder sessionFactoryBuilder = BeanDefinitionBuilder.rootBeanDefinition(org.mybatis.spring.SqlSessionFactoryBean.class);
sessionFactoryBuilder.addPropertyReference("dataSource", "dataSource" + beanName);
sessionFactoryBuilder.addPropertyValue("configLocation", "/sqlmap.xml");
context.registerBeanDefinition("sqlSessionFactory" + beanName, sessionFactoryBuilder.getBeanDefinition());

BeanDefinitionBuilder transactionManagerBuilder = BeanDefinitionBuilder.rootBeanDefinition(org.springframework.jdbc.datasource.DataSourceTransactionManager.class);
transactionManagerBuilder.addPropertyReference("dataSource", "dataSource" + beanName);
context.registerBeanDefinition("transactionManager",transactionManagerBuilder.getBeanDefinition());

ctx.refresh();

1 个答案:

答案 0 :(得分:0)

请尝试在下面添加以下代码,看看它是否有效。

ConfigurableListableBeanFactory factory = context.getBeanFactory();
AspectJAwareAdvisorAutoProxyCreator aspectJPostProcessor = new AspectJAwareAdvisorAutoProxyCreator();
aspectJPostProcessor.setBeanFactory( factory );
aspectJPostProcessor.setProxyClassLoader( context.getClassLoader() );
factory.addBeanPostProcessor(aspectJPostProcessor);