0

使用分布式 Mysql 数据库时,我使用 BeanDefinitionBuilder 创建了多个数据源、会话工厂但是当我使用下面的 getBean('bean name') 方法执行插入 SQL 时,事务注释似乎不起作用

((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();
4

1 回答 1

0

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

ConfigurableListableBeanFactory factory = context.getBeanFactory();
AspectJAwareAdvisorAutoProxyCreator aspectJPostProcessor = new AspectJAwareAdvisorAutoProxyCreator();
aspectJPostProcessor.setBeanFactory( factory );
aspectJPostProcessor.setProxyClassLoader( context.getClassLoader() );
factory.addBeanPostProcessor(aspectJPostProcessor);
于 2013-01-27T10:57:48.517 回答