Spring在应用程序上下文中找不到事务管理器

时间:2017-11-13 15:43:25

标签: java spring hibernate jpa

我使用spring 4.2.6构建了这个多模块Web应用程序,如果我使用tomcat7 maven插件运行它将运行完美,我可以到达控制器甚至从数据源读取数据但是如果我尝试在eclipse tomcat7上运行它服务器我将得到此异常,应用程序无法启动:

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:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:381)
at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d77bfca6.transactionInterceptor(<generated>)
at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration.transactionAdvisor(ProxyTransactionManagementConfiguration.java:45)
at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d77bfca6.CGLIB$transactionAdvisor$0(<generated>)
at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d77bfca6$$FastClassBySpringCGLIB$$7634146e.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
at org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d77bfca6.transactionAdvisor(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 42 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:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 61 more

这是我的pom:

<properties>
    <webapp.name>users-api</webapp.name>
    <hibernate.version>4.3.6.Final</hibernate.version>
    <spring.version>4.2.6.RELEASE</spring.version>
</properties>
<!-- <parent>
    <groupId>com.tecnocasa</groupId>
    <artifactId>users</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent> -->
<dependencies>
    <!-- START SPRING DEPENDENCIES -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
        <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- END SPRING DEPENDENCIES -->
    <!-- START OTHER DEPENDENCIES -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.196</version>
    </dependency>
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>myproject-serviceimpl</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>utils</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <optional>true</optional>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>com.vividsolutions</groupId>
        <artifactId>jts-core</artifactId>
        <version>1.14.0</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
        <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>2.1.1</version>
        <scope>provided</scope>
    </dependency>
    <!-- END OTHER DEPENDENCIES -->
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>myproject-service</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

这是我的WebApplication类:

public class WebApplication implements WebApplicationInitializer{
@Override
public void onStartup( ServletContext servletContext ) throws 
ServletException
{
    AnnotationConfigWebApplicationContext applicationContext = new 
    AnnotationConfigWebApplicationContext();
    AnnotationConfigWebApplicationContext rootContext = new 
    AnnotationConfigWebApplicationContext();
    servletContext.addListener( new ContextLoaderListener( rootContext ) );
    applicationContext.register( ControllerApiConfig.class );
    DispatcherServlet dispatcherServlet = new DispatcherServlet( applicationContext );
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet );
    dispatcher.setLoadOnStartup( 1 );
    dispatcher.addMapping( "/" );
    }
}

这是我的CoreConfig类

@EnableTransactionManagement
@EnableJpaRepositories
@Configuration
@Import( value = { ServiceConfig.class, RepositoryConfig.class } )
public class CoreConfig
{
@Bean
public DataSource dataSource( )
{
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName( "oracle.jdbc.driver.OracleDriver" );
    dataSource.setUrl( "jdbc:oracle:thin:@testdb:1522/myproject" );
    dataSource.setUsername( "user" );
    dataSource.setPassword( "psw" );
    return dataSource;
}
Properties additionalProperties( )
{
    Properties properties = new Properties();
    properties.setProperty( "hibernate.dialect","org.hibernate.dialect.Oracle10gDialect" );
    return properties;
}
@Bean
public EntityManagerFactory entityManagerFactory( )
{
    HibernateJpaVendorAdapter vendorAdapter = newHibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl( false );
    vendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle10gDialect" );
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter( vendorAdapter );
    factory.setPackagesToScan( "com.myproject.dto" );
    factory.setDataSource( dataSource() );
    factory.afterPropertiesSet();
    return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager( )
{
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory( entityManagerFactory() );
    return transactionManager;
}
@Bean
public TransactionTemplate transactionTemplate( PlatformTransactionManager transactionManager )
{
    return new TransactionTemplate( transactionManager );
}
}

0 个答案:

没有答案