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...........
SessionFactory
bean to inject into theCustomerDAOImpl
bean. Instantiate on in your config.