Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.1.2.RELEASE + Hibernate 4.1.7.Final in Struts2 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.1.2.RELEASE + Hibernate 4.1.7.Final in Struts2

    Hi,

    First time here. Learning and Struggling. I have Struts2 web app with Spring for DI & Hibernate. It was working fine with Hibernate3 now want to make it work with Spring 3.1.2.RELEASE + Hibernate 4.1.7.

    I have tried alot with different Maven dependencies for SPRING but no success. Below is error and my application configuration. Someone please help

    Code:
    SEVERE: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/convert/support/PropertyTypeDescriptor
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    	at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:710)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:410)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
    	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
    	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:662)
    Caused by: java.lang.NoClassDefFoundError: org/springframework/core/convert/support/PropertyTypeDescriptor
    	at
    MAVEN pom.xml

    Code:
        <properties>
            <java.version>1.6</java.version>
            <slf4j.version>1.6.6</slf4j.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <hibernate.core.version>4.1.7.Final</hibernate.core.version>
            <spring.version>3.1.2.RELEASE</spring.version>
        </properties>
    
      <dependencies>
      	
    	<!-- jUnit -->
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.10</version>
          </dependency>
    
          <!-- javaee-api 6 dependencies -->
          <dependency>
              <groupId>javax</groupId>
              <artifactId>javaee-api</artifactId>
              <version>6.0</version>
              <scope>provided</scope>
          </dependency>
    
          <!-- struts2 dependencies -->
          <dependency>
              <groupId>org.apache.struts</groupId>
              <artifactId>struts2-core</artifactId>
              <version>2.3.4.1</version>
          </dependency>
          <!-- Struts 2 + Spring plugins -->
          <dependency>
              <groupId>org.apache.struts</groupId>
              <artifactId>struts2-spring-plugin</artifactId>
              <version>2.3.4.1</version>
          </dependency>
    
          <!-- JDBC dependencies -->
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.21</version>
          </dependency>
    
          <!-- Spring dependencies -->
         <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>${spring.version}</version>
          </dependency>
    
    
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-orm</artifactId>
              <version>${spring.version}</version>
          </dependency>
    
    
          <!-- Hibernate dependencies -->
          <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-core</artifactId>
              <version>${hibernate.core.version}</version>
          </dependency>
          <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-entitymanager</artifactId>
              <version>${hibernate.core.version}</version>
          </dependency>
          <dependency>
              <groupId>org.hibernate</groupId>
              <artifactId>hibernate-c3p0</artifactId>
              <version>${hibernate.core.version}</version>
          </dependency>
    
          <!-- Spring AOP dependency -->
          <dependency>
              <groupId>cglib</groupId>
              <artifactId>cglib</artifactId>
              <version>2.2.2</version>
              <scope>runtime</scope>
          </dependency>
    
      </dependencies>
    accplicationContext.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
        <!--its core purpose is to bootstrap a SessionFactory from annotation scanning = LocalSessionFactoryBean.-->
        <!-- The singleton hibernate session factory -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />
        </bean>
    
        <!-- defining transaction manager bean using Hibernate -->
        <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
        <!-- enable the configuration of transactional behavior based on annotations -->
        <tx:annotation-driven transaction-manager="txManager"/>
    
        <!-- Translates Hibernate exceptions to Spring Data Access Exceptions -->
        <bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
    
        <bean id="customerAction" class="com.mkyong.customer.action.CustomerAction">
            <property name="customerBo" ref="customerBo" />
        </bean>
    
        <bean id="customerBo" class="com.mkyong.customer.bo.impl.CustomerBoImpl" >
            <property name="customerDAO" ref="customerDAO" />
        </bean>
    
        <!-- The singleton business services class, to be given to the actions -->
        <bean id="customerDAO" class="com.mkyong.customer.dao.impl.CustomerDAOImpl"  >
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    </beans>
    Web.xml

    Code:
    <!-- Spring -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <listener> <!-- Required for the struts2 spring plugin to work -->
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <filter> <!-- Get spring to keep the session open for the whole request, so hibernate's lazy loads work -->
            <filter-name>openSessionInViewFilter</filter-name>
            <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>openSessionInViewFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    CustomerDAIImpl.java

    Code:
    package com.mkyong.customer.dao.impl;
    
    import com.mkyong.customer.dao.CustomerDAO;
    import com.mkyong.customer.model.Customer;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.util.List;
    
    @Transactional
    public class CustomerDAOImpl implements CustomerDAO{
    
        protected SessionFactory sessionFactory;
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        // Shortcut for sessionFactory.getCurrentSession()
        protected Session session() {
            return sessionFactory.getCurrentSession();
        }
    
        //add the customer
        public void addCustomer(Customer customer){
    
            session().saveOrUpdate(customer);
    
        }
    
        //return all the customers in list
        @Transactional(readOnly = true)
        public List<Customer> listCustomer(){
            return session().createQuery("from Customer").list();
        }
    
    }

  • #2
    Your stacktrace leads me to think you are either missing a dependency or are mixing spring versions. Try to list your dependencies (I suspect a different spring version coming with the struts2-spring-plugin).

    Comment


    • #3
      Thanks alot. Problem is with the Jars and fixed it. 4 small basic questions. Sorry for the trouble.

      1 - Where we use packagesToScan. Should I use it in my situation. Without it code is working?
      2 - Should I use scope="singleton" on sessionfactory. What it really does?

      Code:
       <!-- The singleton hibernate session factory -->
          <bean id="sessionFactory" scope="singleton"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
          </bean>
      3 - Should I use connection pooling with hibernate (c3p0)?
      4 - I want @autowire with Spring in Struts2 web application. Any link to explore that?

      Comment


      • #4
        Where we use packagesToScan. Should I use it in my situation. Without it code is working?
        It depends if you use @Entity and want your entities to be picked up instead of listing all of them you have to use packagesToScan.

        Should I use scope="singleton" on sessionfactory. What it really does?
        No as that is already the default . For what it does I suggest a read of the reference guide which explains bean scopes..

        Should I use connection pooling with hibernate (c3p0)?
        Not sure what you mean with this, if you want to know if you need a connection pool yes unless you want an application that doesn't perform don't use a connectionpool. If you also want to know if you need/want to use the internal connection pooling for hibernate I would advice against it and simply configure a datasource which supports connection pooling and inject that into your sessionfactorybean. That way you can also use it for the cases you need access to plain JDBC.

        I want @autowire with Spring in Struts2 web application. Any link to explore that?
        That is what the struts-spring integration is for and for that you really have to check the struts2 website and documentation.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          If you also want to know if you need/want to use the internal connection pooling for hibernate I would advice against it and simply configure a datasource which supports connection pooling and inject that into your sessionfactorybean. That way you can also use it for the cases you need access to plain JDBC.
          .
          Thanks, very useful information.

          Internal connection pooling for hibernate thats what I wanted to ask. I understand.

          Configure a datasource which is JNDI right?
          I have one confusion. JNDI is configured in the container. Different for different containers. Do you think we have accesses to container in production environment where we configure it for JNDI. I am new and don't know it?

          Comment


          • #6
            Configure a datasource which is JNDI right?
            Whatever you want.. JNDI or just in the applicaitoncontext.

            I have one confusion. JNDI is configured in the container. Different for different containers. Do you think we have accesses to container in production environment where we configure it for JNDI. I am new and don't know it?
            JNDI is just JNDI... Which container doesn't matter (from the perspective of doing a lookup, configuration is different but the way to use is the same).

            Comment

            Working...
            X