我是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>
有人能帮我解决这个问题吗?
您已经将接口和impl标记为存储库。尝试删除客户DAO上的
。@Repository
注释。java
我有以下代码和结构。我得到以下错误,这是很长的错误消息。 创建名称为'departmentController'的bean时出错:通过字段'departmentService'表示的不满意的依赖项;嵌套异常org.springframework.beans.factory.不满意依赖异常: 实体类 存储库接口 服务等级 控制器类 主课 项目结构 完整错误堆栈跟踪:
当我试图进入主页时,我有以下错误 在我的UserController中,我有以下代码 我的用户服务 我的UserServiceImpl 我的假设 web.xml 我的servlet上下文 根上下文为空。 我不知道哪里是可能的错误原因,我试图找到不同的选项,但如果我使用DAO模式,我会收到相同的错误,所以我想知道哪个是问题来解决它。 该项目的配置使用xml,但我认为解决这种情况并不重要。 问候!
以下是跟踪: org.springframework.beans.factory.未满足的DependencyException:创建名称为'testController'的bean时出错:通过字段'testDAO'表示的依赖项未满足;嵌套异常org.springframework.beans.factory.BeanCreationException:创建名称为'testDAO'的bean时出错
我是sping-mvc的新手。我试图找到使用sping-mvc、hiberNate、mysql和jsf开发测试项目的好教程。最后我创建了示例项目。 我的第一个问题是, 就是这个Spring-mvc结构 但不幸的是,上面显示的是错误。 应用程序上下文。xml 人脸配置。xml web.xml 个人类 PersonDAOImpl类 个人服务Impl类 ManagedBeanClass类 错误是, 请给
我查了一些类似的问题,但这些答案帮不了我。 错误 组织。springframework。豆。工厂未满足的依赖项异常:创建名为“accountController”的bean时出错:未满足的依赖项通过字段“accountService”表示;嵌套的异常是org。springframework。豆。工厂NoSuchBeanDefinitionException:没有类型为“com”的合格bean。服务
我想在我的项目中实现Spring Security性。但不管我怎么做,我总是会犯同样的错误。 我创建了必要的类(,,)。它们在同一个包下,但我得到以下错误。 这是发生问题的的一部分 2018-12-31 23:58:10.616信息9952---[main]j.LocalContainerEntityManagerFactoryBean:初始化了持久性单元“默认”的JPA EntityManage