没有名为“ transactionManager”的bean可用:

时间:2020-09-26 22:11:04

标签: java spring spring-boot hibernate spring-mvc

请求处理失败;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的名为“ transactionManager”的bean:找不到与限定符“ transactionManager”匹配的TransactionManager bean-限定符不匹配,bean名称不匹配!

CustomerDAOImpl.java

    @Override
    public int hashCode(){
        if(!hashCodeComputed){
            // or any other sane computation
            hash = 42;
            hashCodeComputed = true;
        }

        return hash;
    }

CustomerController

    // The hash or hashIsZero fields are subject to a benign data race,
    // making it crucial to ensure that any observable result of the
    // calculation in this method stays correct under any possible read of
    // these fields. Necessary restrictions to allow this to be correct
    // without explicit memory fences or similar concurrency primitives is
    // that we can ever only write to one of these two fields for a given
    // String instance, and that the computation is idempotent and derived
    // from immutable state

CustomerDAO

package com.shadow.springdemo.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shadow.springdemo.entity.Customer;

@Repository
public class CustomerDAOImpl implements CustomerDAO {

// need to inject the session factory
@Autowired
private SessionFactory sessionFactory;
        
@Override
@Transactional
public List<Customer> getCustomers() {
    
    // get the current hibernate session
    Session currentSession = sessionFactory.getCurrentSession();
            
    // create a query
    Query<Customer> theQuery = 
            currentSession.createQuery("from Customer", Customer.class);
    
    // execute query and get result list
    List<Customer> customers = theQuery.getResultList();
            
    // return the results       
    return customers;
}

}

spring-mvc-crud-demo-servlet.xml

        package com.shadow.springdemo.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.shadow.springdemo.dao.CustomerDAO;
    import com.shadow.springdemo.entity.Customer;
    
    @Controller
    @RequestMapping("/customer")
    public class CustomerController {
        
        // need to inject the customer dao
        @Autowired(required = true)
        private CustomerDAO customerDAO;
        
        @RequestMapping("/list")
        public String listcustomer(Model theModel) {
            
            //get customer from dao
            List<Customer> theCustomers  = customerDAO.getCustomers();
            
            //add customer to model
            theModel.addAttribute("customers", theCustomers);
            
            return "list-customer";
        }
    }

Web.xml

        package com.shadow.springdemo.dao;
    
    import java.util.List;
    
    import com.shadow.springdemo.entity.Customer;
    
    public interface CustomerDAO {
        
        public List<Customer> getCustomers();
    }

1 个答案:

答案 0 :(得分:1)

引用Spring文档

两个示例之间的细微差别在于 TransactionManager bean:在@Bean情况下,名称为“ txManager” (根据方法的名称);在XML情况下,名称为 “ transactionManager”。 硬连线到 寻找一个名为“ transactionManager”的bean

因此,在您的xml中,将bean myTransaction重命名为transactionManager

        <bean id="transactionManager"
                class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
        
        <!-- Step 4: Enable configuration of transactional behavior based on annotations -->
        <tx:annotation-driven transaction-manager="transactionManager" />
相关问题