I am trying to @Autowire
the SessionFactory
in my GWT Servlet. What I tried first was loading it using the application context:
this.sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
but I guess that doesn't work that way if I want to use the @Transactional
annotation. That's why I'm trying to auto wire it by telling Spring that sessionFactory
is a property to be set over setSessionFactory(SessionFactory sessionFactory);
but that does 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") // Do 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 my 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 I am getting is a org.hibernate.HibernateException
:
Caused by: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:988)
at com.mahlzeit.server.web.repository.impl.RestaurantOwnerRepositoryImpl.get(RestaurantOwnerRepositoryImpl.java:42)
at com.mahlzeit.server.web.repository.impl.RestaurantOwnerRepositoryImpl.getRestaurants(RestaurantOwnerRepositoryImpl.java:74)
at com.mahlzeit.server.web.service.restaurant.RestaurantServiceImpl.getAvailableRestaurants(RestaurantServiceImpl.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:587)
... 25 more
Does anybody know how I can make this work? I found this but I'm not sure if that would work and besides that I am not sure if I want to extend the XsrfProtectedServiceServlet
.
Thank you for any help!
Not familiar with GWT but I wouldn't implement the service in a Servlet. I would have a service layer and inject the bean in the servlet, this way you get a transaction ready service you 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 to XsrfProtectedServiceServlet's javadoc it's an experimental servlet not advised to use in production code:
EXPERIMENTAL and subject to change. Do not use this in production code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.