Having issues autowiring a sessionfactory bean with spring mvc and hibernate -
i trying implement auto-wiring project, seems application isn't seeing sessionfactory definition in application-context.xml when running it.
i'm missing obvious, though i've tried several solutions posts having similar issues no success.
i using spring mvc , hibernate.
here application-context.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="arlua" /> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="prefix"> <value>/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="serverdatasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname"> <value>oracle.jdbc.driver.oracledriver</value> </property> <property name="url"> <value>url</value> </property> <property name="username"> <value>${user}</value> </property> <property name="password"> <value>${pwd}</value> </property> </bean> <bean id="propertyplaceholderconfigurer" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="systempropertiesmodename" value="system_properties_mode_override" /> <property name="ignoreunresolvableplaceholders" value="true"/> </bean> <bean id="mysessionfactory" class="org.springframework.orm.hibernate3.annotation.localsessionfactorybean"> <property name="mappingresources"> <list> <value>mapping/user_info.hbm.xml</value> <value>mapping/login.hbm.xml</value> <value>mapping/linked_accounts.hbm.xml</value> <value>mapping/application_instance.hbm.xml</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="datasource" ref="serverdatasource"/> </bean> <bean id = "userinfodaoimpl" class="arlua.dao.impl.userinfodaoimpl"> <property name="sessionfactory" ref="mysessionfactory"/> </bean> <bean id = "logindaoimpl" class="arlua.dao.impl.logindaoimpl"> <property name="sessionfactory" ref="mysessionfactory"/> </bean> <bean id = "linkedaccountsdaoimpl" class="arlua.dao.impl.linkedaccountsdaoimpl"> <property name="sessionfactory" ref="mysessionfactory"/> </bean> <!-- <bean id = "applicationinstancedaoimpl" class="arlua.dao.impl.applicationinstancedaoimpl"> <property name="sessionfactory" ref="mysessionfactory"/> </bean> --> <!-- ************* transaction management using aop **************** --> <bean id="mytransactionmanager" class="org.springframework.orm.hibernate3.hibernatetransactionmanager"> <property name="sessionfactory" ref="mysessionfactory"/> </bean> <aop:config> <aop:pointcut id="allmethods" expression="execution(* arlua.dao.tableentityfetchdao.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="allmethods"/> </aop:config> <tx:advice id="txadvice" transaction-manager="mytransactionmanager"> <tx:attributes> <tx:method name="saveentity" propagation = "requires_new" isolation = "read_committed" rollback-for = "exception"/> <tx:method name="updateentity" propagation = "requires_new" isolation = "read_committed" rollback-for = "exception"/> <tx:method name="getentity" propagation = "requires_new" isolation = "read_committed" rollback-for = "exception"/> <tx:method name="getallentities" propagation = "requires_new" isolation = "read_committed" rollback-for = "exception"/> </tx:attributes> </tx:advice> </beans>
here controller class trying autowire.
package arlua.controller; import arlua.dao.tableentityfetchdao; import arlua.dao.impl.applicationinstancedaoimpl; import arlua.service.searchcriteria; import arlua.service.useraction; import arlua.tables.applicationinstancetable; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.sessionattributes; import org.springframework.web.servlet.modelandview; @controller @sessionattributes public class searchappcontroller{ @autowired public applicationinstancedaoimpl applicationinstancedaoimpl; private string input; private applicationinstancetable oldapp, newapp; @requestmapping(value = "/search_app_instance", method = requestmethod.post) public string mysearchmethod(@modelattribute("search_criteria") searchcriteria search){ input = search.getinput(); if(input != null) input = input.touppercase(); return "redirect:search_app_instance"; } @requestmapping("/search_app_instance") public modelandview mysuccessmethod(){ modelandview model = new modelandview("search_app_instance"); //check , make sure app exists //applicationcontext factory = // new classpathxmlapplicationcontext("spring-namespace.xml"); //tableentityfetchdao urd = (tableentityfetchdao)factory.getbean("applicationinstancedaoimpl"); try{ applicationinstancetable app = (applicationinstancetable) applicationinstancedaoimpl.getentity(input); oldapp = app; //load app data table model.addobject("app_id", app.getapplication_id()); model.addobject("name", app.getname()); model.addobject("default_exp_period", app.getdefault_expiration_period()); model.addobject("server", app.getserver()); model.addobject("description", app.getdescription()); model.addobject("active", app.getactive()); model.addobject("conn_string", app.getconn_string()); model.addobject("creation_date", app.getcreation_date().gettime()); model.addobject("error", ""); } catch(exception e){ if(input != null) { model.addobject("error", "application not found."); input = ""; } } return model; } @requestmapping(value = "/app_actions", method = requestmethod.post) public string useractionsmethod(@modelattribute("user_action") useraction action, @modelattribute("app_info") applicationinstancetable app_info){ if(action.getaction().equals("update_info")) { newapp = app_info; return "redirect:update_app_info"; } return "redirect:search_app_instance"; } @requestmapping("/update_app_info") public modelandview updateinfomethod(){ modelandview model = new modelandview("search_app_instance"); applicationcontext factory = new classpathxmlapplicationcontext("spring-namespace.xml"); tableentityfetchdao urd = (tableentityfetchdao)factory.getbean("applicationinstancedaoimpl"); newapp.setapplication_id(oldapp.getapplication_id()); newapp.setcreation_date(oldapp.getcreation_date()); urd.updateentity(newapp); model.addobject("msg", "application '" + newapp.getapplication_id() + "' modified successfully."); return model; } }
applicationinstancedaoimpl class
package arlua.dao.impl; import arlua.dao.tableentityfetchdao; import arlua.tables.applicationinstancetable; import java.util.list; import javax.annotation.resource; import org.hibernate.sessionfactory; import org.springframework.stereotype.repository; @repository("applicationinstancedaoimpl") public class applicationinstancedaoimpl extends tableentityfetchdao{ public sessionfactory mysessionfactory; @resource(name = "mysessionfactory") public void setmysessionfactory(sessionfactory mysessionfactory){ this.mysessionfactory = mysessionfactory; } public void saveentity(object applicationinstance) { this.mysessionfactory.getcurrentsession().save((applicationinstancetable)applicationinstance); } public applicationinstancetable getentity(object application_id) { return (applicationinstancetable)this.mysessionfactory.getcurrentsession(). get(applicationinstancetable.class, (string)application_id); } public void updateentity(object applicationinstance) { this.mysessionfactory.getcurrentsession().update((applicationinstancetable)applicationinstance); } public void deleteentity(object applicationinstance) { this.mysessionfactory.getcurrentsession().delete((applicationinstancetable)applicationinstance); } public list<?> getallentities() { return this.mysessionfactory.getcurrentsession().createquery ("from application_instance").list(); } }
tableentityfetchdao class
package arlua.dao; import java.util.list; import org.hibernate.sessionfactory; /* class serve generic blueprint of other data access implementation classes used perform basic crud operations on arlua tables (ie userinfodaoimpl). */ public abstract class tableentityfetchdao { private sessionfactory sessionfactory; public void setsessionfactory(sessionfactory sessionfactory){ this.sessionfactory = sessionfactory; } public abstract void saveentity(final object entity); public abstract object getentity(final object key); public abstract void updateentity(final object entity); public abstract void deleteentity(final object entity); public abstract list<?> getallentities(); }
here of stack trace.
org.springframework.beans.factory.beancreationexception: error creating bean name 'org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping#0': initialization of bean failed; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'searchappcontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: public arlua.dao.impl.applicationinstancedaoimpl arlua.controller.searchappcontroller.applicationinstancedaoimpl; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'applicationinstancedaoimpl': injection of resource dependencies failed; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'mysessionfactory' defined @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:527) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:288) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:190) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:580) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:895) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:425) @ org.springframework.web.context.contextloader.createwebapplicationcontext(contextloader.java:276) @ org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader.java:197) @ org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener.java:47) @ org.apache.catalina.core.standardcontext.listenerstart(standardcontext.java:4206) @ org.apache.catalina.core.standardcontext.start(standardcontext.java:4705) @ org.apache.catalina.core.containerbase.start(containerbase.java:1057) @ org.apache.catalina.core.standardhost.start(standardhost.java:840) @ org.apache.catalina.core.containerbase.start(containerbase.java:1057) @ org.apache.catalina.core.standardengine.start(standardengine.java:463) @ org.apache.catalina.core.standardservice.start(standardservice.java:525) @ org.apache.catalina.core.standardserver.start(standardserver.java:754) @ org.apache.catalina.startup.catalina.start(catalina.java:595) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source) @ java.lang.reflect.method.invoke(unknown source) @ org.apache.catalina.startup.bootstrap.start(bootstrap.java:289) @ org.apache.catalina.startup.bootstrap.main(bootstrap.java:414) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'searchappcontroller': injection of autowired dependencies failed; nested exception org.springframework.beans.factory.beancreationexception: not autowire field: public arlua.dao.impl.applicationinstancedaoimpl arlua.controller.searchappcontroller.applicationinstancedaoimpl; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'applicationinstancedaoimpl': injection of resource dependencies failed; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'mysessionfactory' defined @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:285) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1074) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:517) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:288) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:190) @ org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:1075) @ org.springframework.web.servlet.handler.abstracturlhandlermapping.registerhandler(abstracturlhandlermapping.java:383) @ org.springframework.web.servlet.handler.abstracturlhandlermapping.registerhandler(abstracturlhandlermapping.java:362) @ org.springframework.web.servlet.handler.abstractdetectingurlhandlermapping.detecthandlers(abstractdetectingurlhandlermapping.java:82) @ org.springframework.web.servlet.handler.abstractdetectingurlhandlermapping.initapplicationcontext(abstractdetectingurlhandlermapping.java:58) @ org.springframework.context.support.applicationobjectsupport.initapplicationcontext(applicationobjectsupport.java:119) @ org.springframework.web.context.support.webapplicationobjectsupport.initapplicationcontext(webapplicationobjectsupport.java:72) @ org.springframework.context.support.applicationobjectsupport.setapplicationcontext(applicationobjectsupport.java:73) @ org.springframework.context.support.applicationcontextawareprocessor.invokeawareinterfaces(applicationcontextawareprocessor.java:106) @ org.springframework.context.support.applicationcontextawareprocessor.postprocessbeforeinitialization(applicationcontextawareprocessor.java:85) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.applybeanpostprocessorsbeforeinitialization(abstractautowirecapablebeanfactory.java:394) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1413) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:519) ... 26 more caused by: org.springframework.beans.factory.beancreationexception: not autowire field: public arlua.dao.impl.applicationinstancedaoimpl arlua.controller.searchappcontroller.applicationinstancedaoimpl; nested exception org.springframework.beans.factory.beancreationexception: error creating bean name 'applicationinstancedaoimpl': injection of resource dependencies failed; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'mysessionfactory' defined @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:502) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:84) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor.postprocesspropertyvalues(autowiredannotationbeanpostprocessor.java:282) ... 46 more caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'applicationinstancedaoimpl': injection of resource dependencies failed; nested exception org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'mysessionfactory' defined @ org.springframework.context.annotation.commonannotationbeanpostprocessor.postprocesspropertyvalues(commonannotationbeanpostprocessor.java:300) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.populatebean(abstractautowirecapablebeanfactory.java:1074) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:517) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:222) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:288) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:190) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.findautowirecandidates(defaultlistablebeanfactory.java:844) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.doresolvedependency(defaultlistablebeanfactory.java:786) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.resolvedependency(defaultlistablebeanfactory.java:703) @ org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject(autowiredannotationbeanpostprocessor.java:474) ... 48 more caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no bean named 'mysessionfactory' defined @ org.springframework.beans.factory.support.defaultlistablebeanfactory.getbeandefinition(defaultlistablebeanfactory.java:527) @ org.springframework.beans.factory.support.abstractbeanfactory.getmergedlocalbeandefinition(abstractbeanfactory.java:1083) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:274) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:194) @ org.springframework.context.annotation.commonannotationbeanpostprocessor.autowireresource(commonannotationbeanpostprocessor.java:435) @ org.springframework.context.annotation.commonannotationbeanpostprocessor.getresource(commonannotationbeanpostprocessor.java:409) @ org.springframework.context.annotation.commonannotationbeanpostprocessor$resourceelement.getresourcetoinject(commonannotationbeanpostprocessor.java:541) @ org.springframework.beans.factory.annotation.injectionmetadata$injectedelement.inject(injectionmetadata.java:156) @ org.springframework.beans.factory.annotation.injectionmetadata.inject(injectionmetadata.java:84) @ org.springframework.context.annotation.commonannotationbeanpostprocessor.postprocesspropertyvalues(commonannotationbeanpostprocessor.java:297) ... 59 more
edit: ended fixing problem autowiring interface tableentityfetchdao rather class implementing it, applicationinstancedaoimpl.
so, this
@autowired public applicationinstancedaoimpl applicationinstancedaoimpl;
became this
@autowired public tableentityfetchdao applicationinstancedao;
and seemed fix problem.
instead of:
@resource(name = "mysessionfactory") public void setmysessionfactory(sessionfactory mysessionfactory){ this.mysessionfactory = mysessionfactory; }
use:
@autowired private sessionfactory mysessionfactory;
edit
as of bellow comment:
not sure why method not working guess parent tableentityfetchdao
class has setter of session factory, applicationinstancedaoimpl
- child class overrides , spring reason not it, if tableentityfetchdao
interface, setter injected, @resource
serves same purpose @autowired
difference @resource
allows specify name of bean injected, , @autowire
allows mark bean not required.
to @resource
working try remove setter method parent class. either way makes no sense of having there not used. sessionfactory field. can removed well. class contains abstract methods , can made interface. annotate tableentityfetchdao
@service
annotation better understanding service-layer class.
Comments
Post a Comment