找不到与限定词'transactionManager'相匹配的PlatformTransactionManager bean

时间:2019-02-10 15:36:44

标签: spring hibernate

我使用Hibernate和第一个基于应用程序的教程启动了Spring MVC,并陷入了我的第一个应用程序本身。首先,我遇到了有关未找到bean的错误,然后我在组件扫描中添加了该bean,该组件可以正常工作,但现在与事务管理器bean发生冲突。 感谢您的提前帮助

以下是错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager ' available: No matching PlatformTransactionManager bean found for qualifier 'transactionManager ' - enter code hereneither qualifier match nor bean name match!`enter code here`
    org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:136)
    org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils.qualifiedBeanOfType(BeanFactoryAnnotationUtils.java:95)
    org.springframework.transaction.interceptor.TransactionAspectSupport.determineQualifiedTransactionManager(TransactionAspectSupport.java:406)
    org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManager(TransactionAspectSupport.java:387)
    org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:284)
    org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    com.sun.proxy.$Proxy32.getCustomers(Unknown Source)
    com.crm.controller.CustomerController.listCustomer(CustomerController.java:24)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)



spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.crm" />

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames" value="resources/messages"/>

        </bean>
        <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/> 
        <!-- .cj added extra to run based on console error  -->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mvcmodel?useSSL=false"/>
        <property name="user" value="mvcdemo"/>
        <property name="password" value="mvcdemo"/>


        <property name="minPoolSize" value="5"/>
        <property name="maxPoolSize" value="20"/>
        <property name="maxIdleTime" value="30000"/>
        </bean>

        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="packagesToScan" value="com.crm.entity"/>
        <property name="hibernateProperties">
        <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        </props>
        </property>
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
        </bean>

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

</beans>

    enter code here

CustomerController.java

package com.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.crm.dao.CustomerDAO;
import com.crm.entity.Customer;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerDAO customerDAO;

    @RequestMapping("/list")
    public String listCustomer(Model model)
    {
        //get customers from dao
        List<Customer> customerList = customerDAO.getCustomers();

        //ad customers to model
        model.addAttribute("customers",customerList);
        return "list-customer";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
                http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>dispatcher-mvc</display-name>

 <servlet>
    <servlet-name>dispatcher-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
    <servlet-name>dispatcher-mvc</servlet-name>
    <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>


    I expected the data to be fetched from DB and displayed on jsp page 

1 个答案:

答案 0 :(得分:0)

似乎spring容器没有选择spring-servlet.xml。请确保您提供了正确的命名约定。

dispatcherServletName-servlet.xml

我建议您使用Spring Boot以避免样板代码配置。
需要进一步配置以加载并在侦听器条目和上下文参数下方。

<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-security.xml </param-value> </context-param>