Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringJava配置和@EnableTransactionManagement存在问题_Java_Spring - Fatal编程技术网

SpringJava配置和@EnableTransactionManagement存在问题

SpringJava配置和@EnableTransactionManagement存在问题,java,spring,Java,Spring,我正在从XML配置迁移到Spring上下文配置。相反,当我尝试在Spring 4.0.3.RELEASE Java配置上使用功能等效的@EnableTransactionManagement时,我的Spring上下文无法实例化,出现以下异常: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContext

我正在从XML配置迁移到Spring上下文配置。相反,当我尝试在Spring 4.0.3.RELEASE Java配置上使用功能等效的@EnableTransactionManagement时,我的Spring上下文无法实例化,出现以下异常:

java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:236) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:134) at 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) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:103) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:74) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseMySQLTest.TestConfig': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.transaction.intercep...skipping... at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1558) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ... 45 more Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration.transactionAdvisor()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionInterceptor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'transactionManager' is required at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586) ... 62 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionInterceptor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'transactionManager' is required at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300) at 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() at 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() at 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) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) ... 63 more Caused by: java.lang.IllegalArgumentException: Property 'transactionManager' is required at org.springframework.transaction.interceptor.TransactionAspectSupport.afterPropertiesSet(TransactionAspectSupport.java:195) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) ... 82 more java.lang.IllegalStateException:未能加载ApplicationContext 位于org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99) 位于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.runReflectCall(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) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)中 位于java.lang.reflect.Method.invoke(Method.java:597) 位于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.config.internalTransactionAdvisor”的bean时出错,该名称在类路径资源[org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]:bean实例化失败;嵌套异常是org.springframework.beans.factory.BeanDefinitionStoreException:factory方法[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:factory方法[public org.springframework.transaction.interceptor.beanfactoryTransactionattributeSourceVisor org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration.transactionAdvisor()]引发异常;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“transactionInterceptor”的bean时出错,该bean在类路径资源[org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]中定义:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:需要属性“transactionManager” 位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:188) 位于org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:586) …还有62个 原因:org.springframework.beans.factory.BeanCreationException:创建名为“transactionInterceptor”的bean时出错,该bean在类路径资源[org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:Property' @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {PropertyPlaceholderConfigurer.class, BaseMySQLTest.TestConfig.class}) public class BaseMySQLTest extends AbstractTransactionalJUnit4SpringContextTests { @Configuration @Import(DaoConfig.class) @PropertySource("classpath:/jdbc.properties") @EnableTransactionManagement public static class TestConfig { @Bean public PlatformTransactionManager providesTransactionManager(ListableBeanFactory beanFactory) { return new DataSourceTransactionManager(beanFactory.getBean(DataSource.class)); } } } public class UserDaoImplTest extends BaseMySQLTest { @Autowired private UserDao userDao; @Test public void testById() throws Exception { jdbcTemplate.execute("insert into users (email) values ('bob@example.com')"); ... } } @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {BaseMySQLTest.TestConfig.class}) public class BaseMySQLTest extends AbstractTransactionalJUnit4SpringContextTests { protected int lastInsertId() { return jdbcTemplate.queryForInt("select LAST_INSERT_ID()"); } @Autowired private UserDao userDao; @Test public void testById() throws Exception { jdbcTemplate.execute("insert into mgdb.users (email) values ('bob@example.com')"); int userId = lastInsertId(); Optional xoomUserOptional = userDao.byId(userId); assertThat(xoomUserOptional.isPresent(), equalTo(true)); XoomUser user = xoomUserOptional.get(); assertThat(user.getEmailAddress(), equalTo("bob@example.com")); } @Configuration @Import(DaoConfig.class) @PropertySource("classpath:/jdbc.properties") @EnableTransactionManagement public static class TestConfig { @Bean public PlatformTransactionManager providesTransactionManager(ListableBeanFactory beanFactory) { return new DataSourceTransactionManager(beanFactory.getBean(DataSource.class)); } } }
public static class TestConfig {
    @Autowired
    private DataSource datasource;

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(datasource);
    }
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
    PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    ClassPathResource resource = new ClassPathResource("/META-INF/spring/database.properties");
    placeholderConfigurer.setLocation(resource);
    return placeholderConfigurer;
}