我正在尝试@Autowire
我的GWT Servlet中的SessionFactory
。我首先尝试使用应用程序上下文加载它:
this.sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");
但是如果我想使用@Transactional
注释,我想这不会那样。这就是为什么我试图通过告诉Spring sessionFactory
是一个要设置setSessionFactory(SessionFactory sessionFactory);
的属性来自动连接它,但这不会以某种方式起作用:
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);
}
}
在我的春天servlet-context.xml
我有:
<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>
我得到的是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
有人知道如何让这项工作成功吗?我发现this但我不确定这是否有用,除此之外我不确定是否要扩展XsrfProtectedServiceServlet
。
感谢您的帮助!
答案 0 :(得分:0)
不熟悉GWT,但我不会在Servlet中实现该服务。我会有一个服务层并在servlet中注入bean,这样你就可以使用一个事务就绪服务:
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);
}
}
根据XsrfProtectedServiceServlet's javadoc,它是一个不建议在生产代码中使用的实验性servlet:
实验,可能会有变化。不要在生产中使用它 代码。