【发布时间】:2018-05-20 15:43:20
【问题描述】:
我是 Spring MVC 和 Hibernate 的新手。几天来,我正在尝试制作一个简单的春季项目,但出现了一些错误。错误说 org.springframework.beans.factory.UnsatisfiedDependencyException: Error created bean with name 'CustomerDAOImp'
这是我的错误
HTTP 状态 500 - 内部服务器错误
输入异常报告
消息内部服务器错误
描述服务器遇到阻止它的内部错误 完成此请求。
异常
javax.servlet.ServletException: Servlet.init() 用于 servlet 调度程序 引发异常的根本原因
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“CustomerDAOImp”的 bean 时出错:不满足的依赖关系 通过字段“sessionFactory”表示;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 'org.hibernate.SessionFactory' 类型的合格 bean 可用: 预计至少有 1 个 bean 有资格作为 autowire 候选者。 依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 根本原因
org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 'org.hibernate.SessionFactory' 类型的合格 bean 可用: 预计至少有 1 个 bean 有资格作为 autowire 候选者。 依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)} note 异常的完整堆栈跟踪及其根本原因是 GlassFish Server Open Source Edition 4.1.1 日志中提供。
GlassFish Server 开源版 4.1.1
我使用 maven 多模块来完成这个项目。这里的“CustomerDAOImp”是我在 CustomerDAOImp 类中定义的存储库的名称。这是扩展 GenericImp 类并实现 CustomerDAO 接口的 java 类,进一步 CustomerDAO 扩展了 GenericDAO 接口。
CustomerDAOImp
@Repository(value = "CustomerDAOImp")
public class CustomerDAOImp extends GenericDAOImp<Customer> implements CustomerDAO{
}
客户DAO
public interface CustomerDAO extends GenericDAO<Customer>{
}
通用DAO
public interface GenericDAO<T> {
void insert(T t);
void update(T t);
boolean delete(int id);
List<T> getAll();
T getById(int id);
}
还有我用于映射jsp页面的控制器
@Controller
public class DefaultController {
@Autowired
CustomerDAOImp customerDaoImp;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
return "user/dairy/index";
}
@RequestMapping(value = "/customer", method = RequestMethod.GET)
public String customer(Model model) {
model.addAttribute("customer", customerDaoImp.getAll());
return "user/dairy/customer";
}
}
dispatcher-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-package="com.nishan.dairy"/>
<mvc:annotation-driven/>
<mvc:resources mapping="/static/**" location="/WEB-INF/assets/"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>
</beans>
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/db/jdbc.properties"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.nishan.dairyreport.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
这是我的项目结构
希望得到积极的回应谢谢...........
【问题讨论】:
-
上述问题出在异常中。您没有要注入到
CustomerDAOImplbean 中的SessionFactorybean。在您的配置中实例化。 -
@Head Rush 我已经在配置中添加了SessionFactory
标签: java hibernate spring-mvc