如果使用@Transactional,则jpa repositiry的spring应用程序无法启动

时间:2016-09-21 13:55:22

标签: spring spring-mvc tomcat spring-data-jpa

我正在开发一个使用spring框架的应用程序(4.3.2)。我已配置调度程序servlet(注释驱动)并配置事务管理器和连接相关的bean(我使用JpaRepository访问数据库)。如果我在我的TransactionComponent中使用@Transactional(它使JpaRepository自动装配),在构建应用程序后我无法在tomcat中发布项目。我收到如下错误:

org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina3].StandardHost[localhost].StandardContext[/accounting]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:587)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1798)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [org.apache.catalina.webresources.StandardRoot@281cbe0e]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:4958)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5088)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 10 more
Caused by: java.lang.IllegalArgumentException: The main resource set specified [xxfxxxxfxxxxx\target\OxybinMerchantAccountingService-1.0-SNAPSHOT] is not valid
at org.apache.catalina.webresources.StandardRoot.createMainResourceSet(StandardRoot.java:723)
at org.apache.catalina.webresources.StandardRoot.startInternal(StandardRoot.java:684)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 13 more

21-Sep-2016 18:52:09.918 SEVERE [localhost-startStop-4] org.apache.catalina.startup.HostConfig.deployDescriptor Error deploying configuration descriptor C:\apache-tomcat-8.0.36\conf\Catalina3\localhost\accounting.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component     [StandardEngine[Catalina3].StandardHost[localhost].StandardContext[/accounting]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:587)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1798)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

如果我删除@Transactional,该项目工作正常。请找到我的调度员servlet和下面的web.xml

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd    
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee.xsd    
    ">
<context:component-scan base-package="com.oxybin.oxybinmerchantaccountingservice" />
<jpa:repositories base-package="com.oxybin.oxybinmerchantaccountingservice.abstracts.repositories" />
<mvc:annotation-driven />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
<!--         This makes /META-INF/persistence.xml is no longer necessary -->
    <property name="packagesToScan"   value="com.oxybin.oxybinmerchantaccountingservice.entities" />
 <!--         JpaVendorAdapter implementation for Hibernate EntityManager.
    Exposes Hibernate's persistence provider and EntityManager extension interface -->
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>

    <property name="jpaProperties">
              <props>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
      </property>
</bean>
<tx:annotation-driven   transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/bnbbjj" />
    <property name="username" value="jkkj" />
    <property name="password" value="kjkjk" />

</bean>

和web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee    http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     id="payroll"   version="3.1">
<display-name>OxybinTraderAccountingService</display-name>
<persistence-unit-ref>  
    <persistence-unit-ref-name>persistence/OxybinMerchantPU</persistence-unit-ref-name>  
    <persistence-unit-name>OxybinMerchantPU</persistence-unit-name>  
</persistence-unit-ref>  
<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>        
</servlet-mapping>

 </web-app>

这是我春天的第一次尝试,如果我没有正确配置,请告诉我。我正在使用tomcat。

1 个答案:

答案 0 :(得分:0)

这意味着您的应用程序配置或启动有问题。 我认为问题可以来自空的欢迎文件。

日志中总会有相关信息 - 检查logs / catalina.out并找出问题所在。