【问题标题】:How to solve - Error creating bean with name 'defaultController'如何解决 - 创建名为“defaultController”的 bean 时出错
【发布时间】:2018-05-20 15:43:20
【问题描述】:

我是 Spring MVCHibernate 的新手。几天来,我正在尝试制作一个简单的春季项目,但出现了一些错误。错误说 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>

这是我的项目结构

希望得到积极的回应谢谢...........

【问题讨论】:

  • 上述问题出在异常中。您没有要注入到 CustomerDAOImpl bean 中的 SessionFactory bean。在您的配置中实例化。
  • @Head Rush 我已经在配置中添加了SessionFactory

标签: java hibernate spring-mvc


【解决方案1】:

您是如何创建 ApplicationContext 的? 您是在程序中以编程方式创建它还是希望您的 Spring 声明来处理它?

根据您提供的信息,故障似乎是在调度程序初始化和相应的 DAO 依赖注入期间发生的。 并且 DAO 依赖项已在 applicationContext.xml 中定义,这与 dispatcher-servlet.xml 不同。 所以看起来applicationContext.xml 没有被加载,而dispatcher-servlet.xml 正在加载。

检查您的 web.xml 以确保您有一个 ContextLoader 可以自动实例化 Spring MVC 应用程序的根和 Web 应用程序上下文。

这些行应该存在:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

您可以如上所述初始化上下文或以编程方式进行;有关详细信息,请参阅以下内容: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet-context-hierarchyhttps://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#context-introduction

您还可以检查上下文是否在日志中正确加载。他们将提供有关 bean 初始化的详细信息。 在应用初始化和 servlet 加载阶段共享您的日志消息。

日志记录的一些参考: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

【讨论】:

  • @TT - 非常感谢。在 web.xml 中添加监听器后我的问题解决了
  • @NishanDhungana 答案是 Kiran Tiwari,我刚刚编辑了他/她的答案以提高可读性。
  • @Kiran Tiwari - 感谢您的友好回复
猜你喜欢
  • 2018-05-20
  • 2014-12-15
  • 2021-11-28
  • 2020-10-03
  • 2019-08-28
  • 2015-03-18
  • 2021-03-17
  • 2021-06-18
  • 2019-07-21
相关资源
最近更新 更多