UnknownServiceException:请求未知服务(Hibernate / Spring)

时间:2015-07-29 07:44:03

标签: spring hibernate

这个问题困扰了我好几天..

我尝试从chart1获取当前会话,但收到sessionFactor例外。

我不知道是什么导致这个或这个异常特别意味着什么。 <{1}}被调用时抛出异常。

org.hibernate.service.UnknownServiceException: Unknown service requested

我在我的servlet中调用:

getCurrentSession()

这是我的 servlet-context.xml 文件:

@Component
public class RestaurantOwnerRepository implements RestauranOwnerDAO {

    private SessionFactory sessionFactory;

    public RestaurantOwnerRepository(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    @Transactional
    public List<Restaurant> getAvailableRestaurants(String sessionId) {

        Session session = sessionFactory.getCurrentSession();
        // ..
    }
}

这是我的 hibernate-webserver.cfg.xml 文件:

@Configurable
public class RestaurantInformationServiceImpl  extends XsrfProtectedServiceServlet implements RestaurantInformationService {
    private static final long serialVersionUID = -4088840947018614411L;

    private final static Logger logger = Logger.getLogger(RestaurantInformationServiceImpl.class);

    @Autowired
    public RestauranOwnerDAO restaurantOwnerRepository;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/appServlet/servlet-context.xml");

        this.restaurantOwnerRepository = (RestauranOwnerDAO)applicationContext.getBean("restaurantOwnerRepository");

        ((ConfigurableApplicationContext)applicationContext).close();
    }

    @Override
    public List<RestaurantDTO> getAvailableRestaurants() {

        List<Restaurant> availableRestaurants = restaurantOwnerRepository.getAvailableRestaurants(getSessionId());
        // ..
    }
}

错误消息/堆栈跟踪:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <context:component-scan base-package="com.mahlzeit.web.server" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mahlzeit_db" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <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 transaction-manager="transactionManager"/> -->
    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="restaurantOwnerRepository" class="com.mahlzeit.web.server.dao.RestaurantOwnerRepository">
        <constructor-arg>
            <ref bean="sessionFactory" />
        </constructor-arg>
    </bean>

</beans>

我只是不知道如何解决这个问题。 任何有关这方面的帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

所以,我不知道为什么我要在这条线上发表评论

((ConfigurableApplicationContext)applicationContext).close();

init(ServletConfig config)

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/appServlet/servlet-context.xml");

    this.restaurantOwnerRepository = (RestauranOwnerDAO)applicationContext.getBean("restaurantOwnerRepository");

    //((ConfigurableApplicationContext)applicationContext).close();
}

但是没有关闭应用程序上下文确实在这里提供了帮助..

我没想过这个。我只是按照Eclipse中的警告

  

Resource leak: 'applicationContext' is never closed

然后没有想到就关闭了它。