我正在从Spring配置的XML配置迁移。相反,当我尝试在Spring 4.0.3.RELEASE Java配置上使用功能相同的@EnableTransactionManagement时,我的Spring上下文无法实例化以下异常:
java.lang.IllegalStateException:无法加载ApplicationContext 在org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) 在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) 在org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) 在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238) 在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63) 在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) 在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53) 在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229) 在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 在org.junit.runners.ParentRunner.run(ParentRunner.java:309) 在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175) 在org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236) 在org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134) 在org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:113) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) 在org.apache.maven.surefire.booter.ProviderFactory $ ProviderProxy.invoke(ProviderFactory.java:165) 在org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) 在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103) 在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74) 引起:org.springframework.beans.factory.BeanCreationException:创建名为'baseMySQLTest.TestConfig'的bean时出错:bean的初始化失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / transaction / annotation / ProxyTransactionManagementConfiguration.class]中定义名称为'org.springframework.transaction.config.internalTransactionAdvisor'的bean创建错误:实例化豆子失败了; 嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public org.springframework.transaction.intercep ...跳过... 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ......还有45个 引起:org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration.transactionAdvisor()]引发异常; 嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org / springframework / transaction / annotation / ProxyTransactionManagementConfiguration.class]中定义名称为'transactionInterceptor'的bean时出错:init方法的调用失败; 嵌套异常是java.lang.IllegalArgumentException:属性'transactionManager'是必需的 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586) ......还有62个 引起:org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / transaction / annotation / ProxyTransactionManagementConfiguration.class]中定义的名称为'transactionInterceptor'的bean创建错误:init方法的调用失败; 嵌套异常是java.lang.IllegalArgumentException:属性'transactionManager'是必需的 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:304) 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) at org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:324) at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration $$ EnhancerBySpringCGLIB $$ 83a12634.transactionInterceptor() 在org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration.transactionAdvisor(ProxyTransactionManagementConfiguration.java:45) at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration $$ EnhancerBySpringCGLIB $$ 83a12634.CGLIB $ transactionAdvisor $ 0() at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration $$ EnhancerBySpringCGLIB $$ 83a12634 $$ FastClassBySpringCGLIB $$ cc829ae.invoke() 在org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312) at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration $$ EnhancerBySpringCGLIB $$ 83a12634.transactionAdvisor() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在java.lang.reflect.Method.invoke(Method.java:597) 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) ...还剩63个 引起:java.lang.IllegalArgumentException:属性'transactionManager'是必需的 at org.springframework.transaction.interceptor.TransactionAspectSupport.afterPropertiesSet(TransactionAspectSupport.java:195) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ......还有82个
这恰好在单元测试中获得,但是当它在这里工作时,我可以在生产代码中使用它。
这是我的单元测试基类,其中发生了Spring布线:
@RunWith(SpringJUnit4ClassRunner.class)来 @ContextConfiguration(classes = {PropertyPlaceholderConfigurer.class,BaseMySQLTest.TestConfig.class}) 公共类BaseMySQLTest扩展AbstractTransactionalJUnit4SpringContextTests { @Import(DaoConfig.class) @PropertySource( “类路径:/jdbc.properties”) @EnableTransactionManagement public static class TestConfig { public PlatformTransactionManager提供TransactionManager(ListableBeanFactory beanFactory){ 返回新的DataSourceTransactionManager(beanFactory.getBean(DataSource.class));
这是一个使用这个基类和config的子类:
公共类UserDaoImplTest扩展BaseMySQLTest { @Autowired 私人UserDao用户道; public void testById()抛出异常{ jdbcTemplate.execute(“插入用户(电子邮件)值”('bob@example.com')“);
可以看出,我的Spring TestConfig定义了一个事务管理器bean,根据Javadoc
http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html
意味着我不必命名bean(虽然我已经命名它以试图让它工作)。实际上,Spring上下文的行为就好像配置了XML配置,面对没有明确命名为“transactionManager”的bean。
什么是我的Java Config缺失,以至于Spring上下文不能在实例化时使用此事务管理器bean来满足其要求?
感谢您提供任何有用的意见。
编辑:
(我不确定这个编辑应该去哪里,所以我试试这里.ae6rt)
这是新的测试类,导致与原始工作相同的错误:
@RunWith(SpringJUnit4ClassRunner.class)来 @ContextConfiguration(classes = {BaseMySQLTest.TestConfig.class}) 公共类BaseMySQLTest扩展AbstractTransactionalJUnit4SpringContextTests { protected int lastInsertId(){ return jdbcTemplate.queryForInt(“select LAST_INSERT_ID()”); @Autowired 私人UserDao用户道; public void testById()抛出异常{ jdbcTemplate.execute(“插入mgdb.users(email)值('bob@example.com')”); int userId = lastInsertId(); 可选xoomUserOptional = userDao.byId(userId); assertThat(xoomUserOptional.isPresent(),equalTo(true)); XoomUser user = xoomUserOptional.get(); assertThat(user.getEmailAddress(),equalTo(“bob@example.com”)); @Import(DaoConfig.class) @PropertySource( “类路径:/jdbc.properties”) @EnableTransactionManagement public static class TestConfig { public PlatformTransactionManager提供TransactionManager(ListableBeanFactory beanFactory){ 返回新的DataSourceTransactionManager(beanFactory.getBean(DataSource.class));
我没有在这里需要属性配置
@ContextConfiguration(classes = {BaseMySQLTest.TestConfig.class})
所以我删除了它。希望这符合这一轮的精神。
发布于 2019-06-10 10:53:01
看起来唯一的变化应该是将事务管理器bean名称命名为“transactionManager”:
public static class TestConfig { @Autowired private DataSource datasource; @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(datasource);
编辑 :
你可以尝试这些额外的东西:
0.1。 PropertyPlaceHolderConfigurer 从这里删除.. @ContextConfiguration(classes = {PropertyPlaceholderConfigurer.class, BaseMySQLTest.TestConfig.class}) ,这不是如何使用PropertyPlaceholderConfigurer,你必须这样做:
PropertyPlaceHolderConfigurer
@ContextConfiguration(classes = {PropertyPlaceholderConfigurer.class, BaseMySQLTest.TestConfig.class})
@Bean public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {