1
votes

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

**Here is my project structure**

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

1
The problem as stated is in the exception. You don't have a SessionFactory bean to inject into the CustomerDAOImpl bean. Instantiate on in your config.The Head Rush
@The Head Rush I have already added the SessionFactory in configElon Musk

1 Answers

2
votes

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