繁体   English   中英

如何将SessionFactory自动关联到Servlet?

[英]How to @Autowire a SessionFactory to a Servlet?

我试图在我的GWT Servlet中@Autowire SessionFactory 我首先尝试的是使用应用程序上下文加载它:

this.sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");

但是我想如果我想使用@Transactional批注,那行不通。 这就是为什么我通过告诉Spring sessionFactory是要在setSessionFactory(SessionFactory 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);
    }
}

在我的Spring 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

有人知道我该怎么做吗? 我找到了这个,但是我不确定是否可以使用,除此之外,我不确定是否要扩展XsrfProtectedServiceServlet

感谢您的任何帮助!

不熟悉GWT,但我不会在Servlet中实现该服务。 我将拥有一个服务层,并将Bean注入servlet中,这样您就可以使用可以使用事务处理的服务:

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的javadoc,这是不建议在生产代码中使用的实验性servlet:

实验性和随时更改。 不要在生产代码中使用此代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM