java - How to @Autowire a SessionFactory to a Servlet? -


i trying @autowire sessionfactory in gwt servlet. tried first loading using application context:

this.sessionfactory = (sessionfactory) applicationcontext.getbean("sessionfactory"); 

but guess doesn't work way if want use @transactional annotation. that's why i'm trying auto wire telling spring sessionfactory property set on setsessionfactory(sessionfactory sessionfactory); not work either somehow:

package com.mahlzeit.server.web.repository.impl; // .. @component public class restaurantserviceimpl  extends xsrfprotectedserviceservlet implements restaurantservice {      public restauranownerrepository restaurantownerrepository;     private sessionfactory sessionfactory;      @autowired      public void setsessionfactory(sessionfactory sessionfactory) {         this.sessionfactory = sessionfactory ;     }      @override     public void init(servletconfig config) throws servletexception {          super.init(config);          @suppresswarnings("resource") // not close application context!         applicationcontext applicationcontext = new classpathxmlapplicationcontext("/appservlet/servlet-context.xml");          //this.sessionfactory = (sessionfactory) applicationcontext.getbean("sessionfactory");         this.restaurantownerrepository = (restauranownerrepository)applicationcontext.getbean("restaurantownerrepository");     }      @transactional     @override     public list<restaurantdto> getavailablerestaurants() {         list<restaurant> availablerestaurants = restaurantownerrepository.getrestaurants(getsessionid());         return convertentity.converrestaurants(availablerestaurants);     } } 

in spring servlet-context.xml i'm having:

<context:component-scan base-package="com.mahlzeit.server.web" />  <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean">     <property name="datasource" ref="datasource" />     <property name="configlocation" value="classpath:hibernate-webserver.cfg.xml" /> </bean>  <tx:annotation-driven/> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager">     <property name="sessionfactory" ref="sessionfactory" /> </bean>  <bean id="restaurantserviceimpl" class="com.mahlzeit.server.web.service.restaurant.restaurantserviceimpl">     <property name="sessionfactory" ref="sessionfactory" /> </bean> 

what getting org.hibernate.hibernateexception:

caused by: org.hibernate.hibernateexception: not obtain transaction-synchronized session current thread     @ org.springframework.orm.hibernate4.springsessioncontext.currentsession(springsessioncontext.java:134)     @ org.hibernate.internal.sessionfactoryimpl.getcurrentsession(sessionfactoryimpl.java:988)     @ com.mahlzeit.server.web.repository.impl.restaurantownerrepositoryimpl.get(restaurantownerrepositoryimpl.java:42)     @ com.mahlzeit.server.web.repository.impl.restaurantownerrepositoryimpl.getrestaurants(restaurantownerrepositoryimpl.java:74)     @ com.mahlzeit.server.web.service.restaurant.restaurantserviceimpl.getavailablerestaurants(restaurantserviceimpl.java:70)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:606)     @ com.google.gwt.user.server.rpc.rpc.invokeandencoderesponse(rpc.java:587)     ... 25 more 

does know how can make work? found this i'm not sure if work , besides not sure if want extend xsrfprotectedserviceservlet.

thank help!

not familiar gwt wouldn't implement service in servlet. have service layer , inject bean in servlet, way transaction ready service can use:

public class restaurantservlet extends httpservet{     @autowired     private restaurantservice service;      @override     public void init(servletconfig config) throws servletexception {         super.init(config);         webapplicationcontext ac = webapplicationcontextutils.getrequiredwebapplicationcontext(config.getservletcontext());         ac.getautowirecapablebeanfactory().autowirebean(this);     } } 

according xsrfprotectedserviceservlet's javadoc it's experimental servlet not advised use in production code:

experimental , subject change. not use in production code.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -