当前位置: 首页 > 知识库问答 >
问题:

创建名为“customerController”的bean时出错:通过字段“customerDAO”表示的未满足的依赖关系;

施锋
2023-03-14

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

创建名为“customerController”的bean时出错:通过字段“customerDAO”表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂未满足的依赖项异常:创建名为“customerDAOImpl”的bean时出错:未满足的依赖项通过字段“sessionFactory”表示;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建在ServletContext资源[/WEB-INF/spring servlet.xml]中定义的名为“sessionFactory”的bean时出错:设置bean属性“dataSource”时无法解析对bean“myDataSource”的引用;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建名为“myDataSource”的bean时出错:查找方法解析失败;嵌套的异常是java。lang.IllegalStateException:未能从类加载器[WebappClassLoader上下文:/spring hibernate集成委托:false存储库]中内省类[com.mchange.v2.c3p0.ComboPooledDataSource]:

顾客爪哇:

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();
    }
}

顾客道。爪哇:

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();
}

客户请求。爪哇:

    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;
    }
}

客户控制器。爪哇:

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";
    }
}

网状物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个答案

沈茂
2023-03-14

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

 类似资料: