簡體   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