首页 文章

创建名为'customerController'的 bean 时出错:通过字段'customerDAO'表示的不满意的依赖关系;

提问于
浏览
0

我是 spring 和 hibernate 框架的新手,我已经在依赖项上给出了@Autowired注释,但我不知道为什么会出现这个错误。

创建名为'customerController'的 bean 时出错:通过字段'customerDAO'表示的不满意的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'customerDAOImpl'的 bean 时出错:通过字段'sessionFactory'表示的不满意依赖;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建在 ServletContext 资源[2]中定义名称为'sessionFactory'的 bean 时出错:在设置 bean 属性'dataSource'时无法解析对 bean'myDataSource'的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为'myDataSource'的 bean 时出错:查找方法解析失败;嵌套异常是 java.lang.IllegalStateException:无法从 ClassLoader 内省 Class [3] [WebappClassLoader 上下文:/spring-hibernate-integration 委托:false 存储库:

Customer.java:

package com.luv2code.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="customer")
public class Customer 
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;
    @Column(name="first_name")
    private String firstName;
    @Column(name="last_name")
    private String lastName;
    @Column(name="email")
    private String email;

    public int getId() 
    {
        return id;
    }
    public void setId(int id) 
    {
        this.id = id;
    }
    public String getFirstName() 
    {
        return firstName;
    }
    public void setFirstName(String firstName) 
    {
        this.firstName = firstName;
    }
    public String getLastName() 
    {
        return lastName;
    }
    public void setLastName(String lastName) 
    {
        this.lastName = lastName;
    }
    public String getEmail()
    {
        return email;
    }
    public void setEmail(String email) 
    {
        this.email = email;
    }
    @Override
    public String toString() 
    {
        return "Customer [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }
    public Customer(int id, String firstName, String lastName, String email) 
    {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }
    public Customer() 
    {
        super();
    }
}

CustomerDAO.java:

package com.luv2code.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.luv2code.entity.Customer;

@Repository
public interface CustomerDAO 
{
    public List<Customer> getCustomers();
}

CustomerDAOImpl.java:

package com.luv2code.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.luv2code.entity.Customer;

@Repository
public class CustomerDAOImpl implements CustomerDAO
{
    // Inject the SessionFactory
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional
    public List<Customer> getCustomers() 
    {
        // Get the current hibernate session
        Session currentSession = sessionFactory.getCurrentSession();

        // Create the query
        Query<Customer> theQuery = currentSession.createQuery("from Customer", Customer.class);

        // Execute and get the results
        List<Customer> customersList = theQuery.getResultList();

        // Return the result
        return customersList;
    }
}

CustomerController.java:

package com.luv2code.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.luv2code.dao.CustomerDAO;
import com.luv2code.entity.Customer;

@Controller
@RequestMapping("/customer")
public class CustomerController 
{
    @Autowired
    private CustomerDAO customerDAO;

    @RequestMapping("/list")
    public String listCustomers(Model theModel)
    {
        List<Customer> customers = customerDAO.getCustomers();
        theModel.addAttribute("customersList", customers);
        return "list-customers";
    }
}

web.xml:

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

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

spring-servlet.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:c="http://www.springframework.org/schema/c"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- Add support for component scanning -->
    <context:component-scan base-package="com.luv2code" />

    <!-- Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven />

    <!-- Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Step 1: Define Database DataSource / connection pool -->
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
        <property name="jdbcUrl"
            value="jdbc:mysql://localhost:3306/spring_hibernate_integration" />
        <property name="user" value="root" />
        <property name="password" value="root" />

        <!-- these are connection pool properties for C3P0 -->
        <property name="initialPoolSize" value="5" />
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxIdleTime" value="30000" />
    </bean>

    <!-- Step 2: Setup Hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.luv2code.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- Step 3: Setup Hibernate transaction manager -->
    <bean id="myTransactionManager"
        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="myTransactionManager" />

    <!-- Add support for reading web resources: css, images, js, etc ... -->
    <!-- <mvc:resources location="/resources/" mapping="/resources/**"></mvc:resources> -->

</beans>

任何人都可以帮我解决这个问题吗?

1 回答

  • 0

    您已将接口和 impl 标记为存储库。尝试删除CustomerDAO.java上的@Repository注释。

相关问题