如何解决 - 创建名为'defaultController'的bean时出错 [英] How to solve - Error creating bean with name 'defaultController'

查看:1372
本文介绍了如何解决 - 创建名为'defaultController'的bean时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring MVC Hibernate 的新手。几天我试图做一个简单的春季项目,但在那里遇到了一些错误。错误说明 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'CustomerDAOImp'的bean

错误

这是我的错误


HTTP状态500 - 内部服务器错误



类型例外报告



messageInternal Server Error



description服务器遇到内部错误,导致
无法完成此请求。



异常



javax.servlet.ServletException:Servlet调度程序的Servlet.init()
抛出异常根本原因



org.springframework.beans.factory.UnsatisfiedDependencyException:
创建名为'CustomerDAOImp'的bean时出错:不满意的依赖关系
通过字段'sessionFactory'表示;嵌套异常是
org.springframework.beans.factory.NoSuchBeanDefinitionException:No
类型为'org.hibernate.SessionFactory'的限定bean可用:
预计至少有1个bean可以作为autowire候选者。
依赖注释:
{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
根本原因



org.springframework.beans.factory.NoSuchBeanDefinitionException:No
类型为'org.hibernate.SessionFactory'的限定bean可用:
预计至少有1个bean可以作为autowire候选者。
依赖注释:
{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
note异常的完整堆栈跟踪及其根本原因是
可在GlassFish Server开源4.1.1日志中找到。



GlassFish Server开源版4.1.1


我使用maven multi模块完成了这个项目。这里'CustomerDAOImp'是我在CustomerDAOImp类中定义的存储库的名称。这是扩展GenericImp类并实现CustomerDAO接口的java类,并且进一步的CustomerDAO扩展了GenericDAO接口。



CustomerDAOImp

  @Repository( value =CustomerDAOImp)
公共类CustomerDAOImp扩展GenericDAOImp< Customer>实施CustomerDAO {

}

CustomerDAO

 公共接口CustomerDAO扩展GenericDAO< Customer> {

}

GenericDAO

 公共接口GenericDAO< T> {
void insert(T t);
void update(T t);
boolean delete(int id);
列表< T>得到所有();
T getById(int id);
}

我的控制器用于映射jsp页面

  @Controller 
公共类DefaultController {
@Autowired
CustomerDAOImp customerDaoImp;

@RequestMapping(value =/,method = RequestMethod.GET)
public String index(){
returnuser / dairy / index;
}

@RequestMapping(value =/ customer,method = RequestMethod.GET)
public String customer(Model model){
model.addAttribute( customer,customerDaoImp.getAll());
返回user / dairy / customer;
}
}

dispatcher-servlet.xml

 <?xml version =1.0encoding =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 =viewResolverclass =org.springframework.web.servlet.view.InternalResourceViewResolver
p:prefix =/ WEB-INF / views /p:suffix JSP= />

< / beans>

applicationContext.xml

 <?xml version =1.0encoding =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 =propertyConfigurerclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
p:location =/ WEB-INF / db / jdbc.properties/>

< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource
p:driverClassName =$ {jdbc.driverClassName}p:url =$ {jdbc.url}
p:username =$ {jdbc.username}p:password =$ {jdbc.password}/>

< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =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>

这是我的项目结构





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

解决方案

你是如何创建ApplicationContext的?
您是否已在程序中以编程方式创建它或者期望您的Spring声明能够处理它?<​​/ p>

从您提供的内容看,它看起来像失败在调度程序初始化和相应的DAO依赖注入期间发生。
并且在 applicationContext.xml 中定义了DAO依赖项,这与 dispatcher-servlet.xml 不同。
因此,当 dispatcher-servlet.xml 正在加载时,似乎没有加载 applicationContext.xml



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



这些行应该存在:

 < ;听者GT; 
< 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-hierarchy

https:// 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


I am new to Spring MVC and Hibernate. For several days i am trying to make a simple spring project but got some error there. The error says org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerDAOImp'

Here is my error

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception root cause

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'CustomerDAOImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} root cause

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.

GlassFish Server Open Source Edition 4.1.1

I have used maven multi module to done this project. Here 'CustomerDAOImp' is the name of repository that i have defined in the CustomerDAOImp class. This is the java class that extends GenericImp class and implements CustomerDAO interface and further CustomerDAO extends the GenericDAO interface.

CustomerDAOImp

@Repository(value = "CustomerDAOImp")
public class CustomerDAOImp extends GenericDAOImp<Customer> implements CustomerDAO{

}

CustomerDAO

public interface CustomerDAO extends GenericDAO<Customer>{

}

GenericDAO

public interface GenericDAO<T> {
    void insert(T t);
    void update(T t);
    boolean delete(int id);
    List<T> getAll();
    T getById(int id);
}

And my controller for mapping jsp page

@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>

Here is my project structure

Hoping for the positive response thanks...........

解决方案

How have you created the ApplicationContext? Have you created it programmatically in your program or are expecting your Spring declarations to take care of it?

From what you have provided, it looks like the failure is occuring during dispatcher initialization and the corresponding DAO dependency injection. And the DAO dependencies have been defined in applicationContext.xml which is different from dispatcher-servlet.xml. So seems like applicationContext.xml is not being loaded while dispatcher-servlet.xml is loading up.

Check your web.xml to ensure that you have a ContextLoader that automatically instantiates your root and web application contexts for Spring MVC app.

These lines should be present:

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

You can initialize your contexts as above or do it programmatically; refer to below for more details: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet-context-hierarchy and https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#context-introduction

You can also check if the contexts are loading properly in the logs. They would provide details on the bean initialization. Share your log messages during the app initialization and servlet loading phase.

Some references for logging: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

这篇关于如何解决 - 创建名为'defaultController'的bean时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆