0

When Tomcat starts I get this messages:

    INFO: Initializing Spring FrameworkServlet 'spring'
2012-06-20 17:02:55,209 INFO [org.springframework.web.servlet.DispatcherServlet] - <FrameworkServlet 'spring': initialization started>
2012-06-20 17:02:55,243 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - <Refreshing WebApplicationContext for namespace 'spring-servlet': startup date [Wed Jun 20 17:02:55 CEST 2012]; root of context hierarchy>
2012-06-20 17:02:55,305 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from ServletContext resource [/WEB-INF/spring-servlet.xml]>



2012-06-20 17:02:56,094 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - <Bean 'hibernateConfiguration' of type [class com.xxxxxxxxxx.android.market.config.HibernateConfiguration$$EnhancerByCGLIB$$53f79727] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)>
2012-06-20 17:02:56,690 INFO [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] - <Building new Hibernate SessionFactory>
2012-06-20 17:02:57,285 INFO [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] - <Updating database schema for Hibernate SessionFactory>
2012-06-20 17:02:57,621 INFO [org.springframework.web.context.support.XmlWebApplicationContext] - <Bean 'sessionFactoryBean' of type [class org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)>


2012-06-20 17:02:57,694 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@46ed5d9d: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,beanConfiguration,hibernateConfiguration,AppService,appController,homeController,jspViewResolver,messageSource,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,appCategory,appDAO,appReleaseDAO,appService,sessionFactoryBean,transactionManager,persistenceExceptionTranslationPostProcessor]; root of factory hierarchy>
2012-06-20 17:02:58,011 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - <Mapped URL path [/{id}] onto handler 'appController'>
2012-06-20 17:02:58,012 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - <Mapped URL path [/{id}.*] onto handler 'appController'>
2012-06-20 17:02:58,012 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - <Mapped URL path [/{id}/] onto handler 'appController'>
2012-06-20 17:02:58,012 INFO [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping] - <Root mapping to handler 'homeController'>
2012-06-20 17:02:58,482 INFO [org.springframework.web.servlet.DispatcherServlet] - <FrameworkServlet 'spring': initialization completed in 3273 ms>
Jun 20, 2012 5:02:58 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jun 20, 2012 5:02:58 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 20, 2012 5:02:58 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/122  config=null
Jun 20, 2012 5:02:58 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 4237 ms

Here is my Hibernate config class. It's annotated based and when I use inside of the Junit tets @ContextConfiguration all works.

    package com.xxxxxxxxxxxxx.android.market.config;

import java.util.Properties;

import org.hibernate.dialect.MySQL5InnoDBDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;

import com.xxxxxxxxxxxxx.android.market.dao.xxx;
import com.xxxxxxxxxxxxx.android.market.dao.xxx;
import com.xxxxxxxxxxxxx.android.market.dao.xxx;

@Configuration
public class HibernateConfiguration {

    @Bean
    public AnnotationSessionFactoryBean sessionFactoryBean() {
        Properties props = new Properties();
        props.put("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        props.put("hibernate.connection.password", "philipp");
        props.put("hibernate.connection.url", "jdbc:mysql://localhost/Market");
        props.put("hibernate.connection.username", "philipp");

        AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
        bean.setAnnotatedClasses(new Class[] { xxx.class, xxx.class, xxx.class });
        bean.setHibernateProperties(props);
        bean.setSchemaUpdate(true);
        return bean;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager(sessionFactoryBean().getObject());
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}

My spring-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />

    <!-- eingefügt aus app-context -->
    <context:component-scan base-package="com.xxxxxxxxxxxxx.android.market.config" />
    <context:component-scan base-package="com.xxxxxxxxxxxxx.android.market.market.dao" />
    <context:component-scan base-package="com.xxxxxxxxxxxxx.android.market.service.app" />
    <context:component-scan base-package="com.xxxxxxxxxxxxx.android.market.controller" />
    <!-- eingefügt aus app-context -->

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

Now, when will get some items by rest-api from the database I get this:

    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
    org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685)
    com.xxxxxxxxxxxxx.android.market.dao.AppDAOImpl.currentSession(AppDAOImpl.java:22)
    com.xxxxxxxxxxxxx.android.market.dao.AppDAOImpl.getAppById(AppDAOImpl.java:46)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    $Proxy21.getAppById(Unknown Source)
    com.xxxxxxxxxxxxx.android.market.service.app.AppServiceImpl.getAppById(AppServiceImpl.java:29)
    com.xxxxxxxxxxxxx.android.market.controller.AppController.getApp(AppController.java:22)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:669)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:574)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)



    package com.xxxxxxxxxxxxx.android.market.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.xxxxxxxxxxxxx.android.market.dao.App;
import com.xxxxxxxxxxxxx.android.market.service.app.AppService;

@Controller
public class AppController {

    @Autowired
    private AppService appService;

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ModelAndView getApp(@PathVariable int id) {
        App app = appService.getAppById(id);
        return new ModelAndView("appXmlView", BindingResult.MODEL_KEY_PREFIX
                + "app", app);
    }
}




    package com.xxxxxxxxxxxxx.android.market.service.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xxxxxxxxxxxxx.android.market.dao.App;
import com.xxxxxxxxxxxxx.android.market.dao.AppDAO;

@Service("AppService")
public class AppServiceImpl implements AppService {

    @Autowired
    private AppDAO appDAO;

    public boolean saveApp(App app) {
        return appDAO.insert(app);
    }

    public void deleteApp(App app) {
        appDAO.delete(app);

    }

    public void updateApp(App app) {
        appDAO.update(app);
    }

    @Transactional
    public App getAppById(int id) {
        return appDAO.getAppById(id);
    }

    public App getAppByName(String name) {
        return appDAO.getAppByName(name);
    }

}




import java.util.Properties;

import org.hibernate.dialect.MySQL5InnoDBDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.xxxxxxxxxxxxxxxx.android.market.dao.App;
import com.xxxxxxxxxxxxxxxx.android.market.dao.AppCategory;
import com.xxxxxxxxxxxxxxxx.android.market.dao.AppRelease;

New HibernateConfiguration file:

@Configuration
@EnableTransactionManagement
public class HibernateConfiguration {

    @Bean
    public AnnotationSessionFactoryBean sessionFactoryBean() {
        Properties props = new Properties();
        props.put("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
        props.put("hibernate.format_sql", "true");
        props.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        props.put("hibernate.connection.password", "philipp");
        props.put("hibernate.connection.url", "jdbc:mysql://localhost/BusinessMarket");
        props.put("hibernate.connection.username", "philipp");

        AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
        bean.setAnnotatedClasses(new Class[] { AppCategory.class, App.class, AppRelease.class });
        bean.setHibernateProperties(props);
        bean.setExposeTransactionAwareSessionFactory(false);
        bean.setSchemaUpdate(true);
        return bean;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        return new HibernateTransactionManager(sessionFactoryBean().getObject());
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

}
4

1 回答 1

0

Since you're using Spring 3.1 Configuration in Java try removing

<tx:annotation-driven transaction-manager="transactionManager" />

and specifying @EnableTransactionManagement on HibernateConfiguration as follows

@Configuration
@EnableTransactionManagement
public class HibernateConfiguration
于 2012-06-20T16:02:00.103 回答