spring - Making sessionFactory available in root context using Java Based Configuration -
i trying learn spring framework. have created sample application using spring-mvc , hibernate.
the problem facing want use opensessioninviewfilter in application getting nosuchbeandefinitionexception when trying hit page.
configuration class -
package com.monil.spring.config; import java.util.properties; import javax.sql.datasource; import org.apache.commons.dbcp.basicdatasource; import org.hibernate.sessionfactory; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.orm.hibernate4.localsessionfactorybuilder; import org.springframework.web.servlet.viewresolver; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.resourcehandlerregistry; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import org.springframework.web.servlet.view.internalresourceviewresolver; import com.monil.spring.model.employee.employee; @configuration @componentscan(basepackages="com.monil.spring") @enablewebmvc public class mvcconfiguration extends webmvcconfigureradapter{ @bean(name = "employeesessionfactory") public sessionfactory getemployeesessionfactory(datasource datasource) { localsessionfactorybuilder factorybuilder = new localsessionfactorybuilder(datasource); factorybuilder.addannotatedclass(employee.class); factorybuilder.addproperties(gethibernateproperties()); return factorybuilder.buildsessionfactory(); } private properties gethibernateproperties() { properties properties = new properties(); properties.put("hibernate.show_sql", "true"); properties.put("hibernate.dialect", "org.hibernate.dialect.mysqldialect"); properties.put("hibernate.current_session_context_class", "thread"); //properties.put("hibernate.hbm2ddl.auto", "create"); return properties; } @bean public datasource getdatasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname("com.mysql.jdbc.driver"); datasource.seturl("jdbc:mysql://localhost:3306/springtestdb3"); datasource.setusername("root"); datasource.setpassword("monils"); return datasource; } @bean public viewresolver getviewresolver(){ internalresourceviewresolver resolver = new internalresourceviewresolver(); resolver.setprefix("/web-inf/views/"); resolver.setsuffix(".jsp"); return resolver; } @override public void addresourcehandlers(resourcehandlerregistry registry) { registry.addresourcehandler("/resources/**").addresourcelocations("/resources/"); } }
here web.xml -
<?xml version="1.0" encoding="utf-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>spring_mvc_hibernate</display-name> <context-param> <param-name>contextclass</param-name> <param-value> org.springframework.web.context.support.annotationconfigwebapplicationcontext </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <servlet> <servlet-name>springdispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextclass</param-name> <param-value> org.springframework.web.context.support.annotationconfigwebapplicationcontext </param-value> </init-param> <init-param> <param-name>contextconfiglocation</param-name> <param-value>com.monil.spring</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springdispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout>30</session-timeout> </session-config> <filter> <filter-name>hibernatefilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class> <init-param> <param-name>sessionfactorybeanname</param-name> <param-value>employeesessionfactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>hibernatefilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
i getting following exception -
severe: servlet.service() servlet [springdispatcher] in context path [/spring_mvc_hibernate] threw exception org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'employeesessionfactory' defined @ org.springframework.beans.factory.support.defaultlistablebeanfactory.getbeandefinition(defaultlistablebeanfactory.java:641) @ org.springframework.beans.factory.support.abstractbeanfactory.getmergedlocalbeandefinition(abstractbeanfactory.java:1159) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:282) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:200) @ org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:979) @ org.springframework.orm.hibernate4.support.opensessioninviewfilter.lookupsessionfactory(opensessioninviewfilter.java:191) @ org.springframework.orm.hibernate4.support.opensessioninviewfilter.lookupsessionfactory(opensessioninviewfilter.java:176) @ org.springframework.orm.hibernate4.support.opensessioninviewfilter.dofilterinternal(opensessioninviewfilter.java:127) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:108) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) @ org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:617) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:518) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1091) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:668) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1521) @ org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1478) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(unknown source)
Comments
Post a Comment