没有为限定符“transactionManager”找到匹配的 PlatformTransactionManager bean

Posted

技术标签:

【中文标题】没有为限定符“transactionManager”找到匹配的 PlatformTransactionManager bean【英文标题】:No matching PlatformTransactionManager bean found for qualifier 'transactionManager' 【发布时间】:2016-02-13 23:55:14 【问题描述】:

我是春天世界的新手。我一直在尝试使用 Spring Data 编写一个基本应用程序,使用 Hibernate 和 mysql 数据库编写 JPA。 我看到了错误:

没有为限定符找到匹配的 PlatformTransactionManager bean '事务管理器'

这是我的 spring-config.xml:

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.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">

    <jpa:repositories base-package="com.levadata.models" />
    <context:component-scan base-package="com.levadata" />

    <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/levadata_beta" />
     <property name="username" value="root" />
    <property name="password" value="root" />
    </bean>

    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

    <bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    </bean>

      <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="jcgPersistence" />
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation" value="META-INF/persistence.xml" />
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    <property name="jpaDialect" ref="jpaDialect" />
    <property name="jpaProperties">
     <props>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
     </property>
  </bean>

    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaDialect" ref="jpaDialect" />
   </bean>

       <tx:annotation-driven transaction-manager="txManager" />

</beans>

这是我的 Employee.java(myPOJO):

@Entity
@Table(name = "employee")
public class Employee 

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "employee_id")
private long employeeId;

@Column(name = "employee_name")
private String employeeName;

public Employee() 


public Employee(String employeeName) 
    this.employeeName = employeeName;


public long getEmployeeId() 
    return this.employeeId;


public void setEmployeeId(long employeeId) 
    this.employeeId = employeeId;


public String getEmployeeName() 
    return this.employeeName;


public void setEmployeeName(String employeeName) 
    this.employeeName = employeeName;


这是我的存储库:

public interface EmployeeRepository extends JpaRepository<Employee, Long>

  Employee save(Employee employee);

  //@Query("select distinct employee_name from employee")
  //public Slice<String> findAllDistinctNames(Pageable pageable);
  public Slice<String> findAllByEmployeeName(Pageable pageable);

最后是我的 POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.levadata</groupId>
<artifactId>exampleLytica</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>exampleLytica</name>

<properties>
    <spring.version>4.2.1.RELEASE</spring.version>
    <spring-data.version>Gosling-RELEASE</spring-data.version>

    <!-- The main class to start by executing java -jar -->
    <start-class>com.levadata.Application</start-class>

</properties> 

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>$spring-data.version</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>


<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>

    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-util</artifactId>
        <version>8.0.0-RC1</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.2.5</version>
    </dependency>

    <!-- JPA Provider (Hibernate) -->

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>
<dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-core</artifactId>
         <version>5.0.2.Final</version>
 </dependency>

 <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>5.0.2.Final</version>
 </dependency>

 <!-- optional -->

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.0.2.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.0.2.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.2.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.0.2.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.0.2.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.2.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

我的主要方法:

public static void main(String[] args)
    
            try
            
                    ApplicationContext context = new ClassPathXmlApplicationContext("spring-configuration.xml");
                    System.out.println("Context : " + context);
                    //Fetch the DAO from Spring Bean Factory
                    EmployeeRepository repository = (EmployeeRepository)context.getBean("employeeRepository");
                    Employee employee = new Employee("Employee123");
                    //employee.setEmployeeId("1");

                    //Save an employee Object using the configured Data source
                    repository.save(employee);
                    System.out.println("Employee Saved with EmployeeId "+employee.getEmployeeId());

                    //find an object using Primary Key
                    Employee emp = repository.findOne(employee.getEmployeeId());
                    System.out.println(emp);

                    //Close the ApplicationContext
                    ((ConfigurableApplicationContext)context).close();

            
            catch (BeansException e)
            
                    e.printStackTrace();
            
    

当我运行代码时,我得到了堆栈跟踪:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined: No matching PlatformTransactionManager bean found for qualifier 'transactionManager' - neither qualifier match nor bean name match!
at org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:100)
at org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:56)
at org.springframework.transaction.interceptor.TransactionAspectSupport.determineQualifiedTransactionManager(TransactionAspectSupport.java:377)
at org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:361)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:271)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy20.save(Unknown Source)
at com.levadata.SpringDataDemo.main(SpringDataDemo.java:26)

任何想法,我在这里缺少哪一块。

【问题讨论】:

在你的EmployeeRepository 实现类中,你有@Transactional 注释吗? EmployeeRepository 没有实现。 Spring 自己实现。 【参考方案1】:

可能是 Spring 想要事务管理器,默认名称是事务管理器。所以尝试将 txManager 重命名为 transactionManager。或者在需要的地方指定当前经理名称。

【讨论】:

.HibernateTransactionManager"> 为我工作。 spring boot 出现同样的错误怎么办?我也给了@Transactional注解 尝试查看初始化日志。数据源是否创建?也许您缺少诸如 spring-boot-starter-jpa 或 JPA 提供程序(Hibernate)之类的东西。

以上是关于没有为限定符“transactionManager”找到匹配的 PlatformTransactionManager bean的主要内容,如果未能解决你的问题,请参考以下文章

const 不兼容的类型限定符问题

自动检测是否需要在函数参数中添加“const”限定符

为啥我应该更喜欢 unsafe_unretained 限定符而不是为弱引用属性赋值? [复制]

通过 hbase shell 命令显示列族和列限定符

第3章—高级装配—创建自定义的限定符

2.4 const限定符