0

Estoy usando para mi proyecto (maven 3.3.9, spring 4+, hibernate4+, tiles 3, postgres 9+, jdk 8, wildfly 10), y estoy recibiendo el siguiente mensaje de error:

Cannot find class [org.springframework.orm.hibernat4.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext

¿Alguien sabe por qué recibo ese error y cómo puedo solucionarlo?


Aquí está mi pom:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cl.legal</groupId>
    <artifactId>opciondefensa</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Opcion Defensa Maven App</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.2.0.RELEASE</spring.version>
        <jstl.version>1.2</jstl.version>
        <tiles.version>3.0.1</tiles.version>
        <hibernate.version>4.1.7.Final</hibernate.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.6.1</slf4j.version>
        <logback.version>0.9.26</logback.version>
        <postgresql.version>9.4.1208</postgresql.version>
        <joda.version>2.2</joda.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${joda.version}</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <!-- Apache Commons Upload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>        
        </dependency>

        <!-- Amazon -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.10.75</version>
        </dependency>

        <!-- Apache Commons Upload -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>            
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-base</artifactId>
            <version>2.4.4</version>
        </dependency>

        <!--  postgres  Connector-->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-extras</artifactId>
            <version>${tiles.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>2.1</version>          
        </dependency>

        <!-- Log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
            <exclusions>
               <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
               </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
            <exclusions>
               <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
               </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>            
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>            
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>                        
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
            <exclusions>
               <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
               </exclusion>
            </exclusions>   
        </dependency>       

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
            <exclusions>
               <exclusion>
                  <groupId>commons-logging</groupId>
                  <artifactId>commons-logging</artifactId>
               </exclusion>
            </exclusions>   
        </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- optional -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>opciondefensa</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
<!--            <plugin> -->
<!--                 <groupId>org.wildfly.plugins</groupId> -->
<!--                 <artifactId>wildfly-maven-plugin</artifactId> -->
<!--                 <version>1.1.0.Alpha5</version> -->
<!--             </plugin> -->
        </plugins>
    </build>
</project>

Éste es mi spring-servlet.xml:

<!-- Configures the @Controller programming model -->
<!--     <mvc:annotation-driven /> -->
<!--     <context:annotation-config /> -->
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- Specifying base package of the Components like Controller, Service, 
        DAO -->
    <context:component-scan base-package="cl.opciondefensa.controller" />
    <context:component-scan base-package="cl.opciondefensa.service" />
    <context:component-scan base-package="cl.opciondefensa.dao" />
    <context:component-scan base-package="cl.opciondefensa.util" />
    <context:component-scan base-package="cl.opciondefensa.entity" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="50000000" />
    </bean>

    <!-- View Resolver -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.tiles3.TilesViewResolver" />

    <!-- Tiles configuration -->
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles-definitions.xml</value>
            </list>
        </property>
    </bean>

<!-- Getting Database properties -->
<!--     <context:property-placeholder location="classpath:application.properties" /> -->
    <context:property-placeholder location="classpath:application.properties" />

<!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
<!--        <property name="driverClass" value="${database.driverClassName}" /> -->
<!--         <property name="jdbcUrl" value="${database.url}" /> -->
<!--         <property name="user" value="${database.username}" /> -->
<!--         <property name="password" value="${database.password}" /> -->
<!--         <property name="acquireIncrement" value="${connection.acquireIncrement}" /> -->
<!--         <property name="minPoolSize" value="${connection.minPoolSize}" /> -->
<!--         <property name="maxPoolSize" value="${connection.maxPoolSize}" /> -->
<!--         <property name="maxIdleTime" value="${connection.maxIdleTime}" /> -->
<!--    </bean> -->
    <!-- DataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="org.postgresql.Driver" />
        <property name="jdbcUrl" value="${database.url}" />
        <property name="user" value="${database.username}" />
        <property name="password" value="${database.password}" />
        <property name="acquireIncrement" value="${connection.acquireIncrement}" />
        <property name="minPoolSize" value="${connection.minPoolSize}" />
        <property name="maxPoolSize" value="${connection.maxPoolSize}" />
        <property name="maxIdleTime" value="${connection.maxIdleTime}" />
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernat4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="cl.opciondefensa.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.connection.characterEncoding">${hibernate.connection.characterEncoding}</prop>
                <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
                <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
                <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>     
                <prop key="hibernate.cache.generate_statistics">${hibernate.cache.generate_statistics}</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
            </props>
        </property>
    </bean>

<!-- Specifying the Resource location to load JS, CSS, Images etc -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!-- Transaction -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

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

</beans>

Y mi WEB.XML :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <display-name>OpcionDefensa</display-name>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Finalmente, application.properties para spring-servlet.xml en sessionFactory:

database.driverClassName=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/opciondefensa
database.username=postgres
database.password=postgres

connection.acquireIncrement=2
connection.minPoolSize=20
connection.maxPoolSize=50
connection.maxIdleTime=600
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.hbm2ddl.auto=create 
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.connection.characterEncoding=utf8
hibernate.cache.use_query_cache=false
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_structured_entries=true
hibernate.cache.generate_statistics=false

El InicioController :

@Controller public class InicioController {

@RequestMapping(value="/", method = RequestMethod.GET)
public ModelAndView home() {
    ModelAndView mav = new ModelAndView("home");
    String url="";
    url = "home";
    mav.setViewName(url);
    return mav;
}

}

1
  • Gracias, por tú edición.
    – Laed
    Commented el 15 jul. 2016 a las 17:46

1 respuesta 1

1

Creo que en el web.xml te falta unir el servlet con su spring-servlet.xml, ¿no?

Te faltaría esto en el web.xml:

<servlet>
   <servlet-name>spring</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/classes/spring-servlet.xml</param-value>
    </init-param>
<load-on-startup>1</load-on-startup>

La ruta la he puesto al azar, comprueba que tu archivo está en esa ruta o cambia la ruta.

Así estarías configurando el servlet.
Si lo que quieres es un contexto para toda la aplicación y no solo para el servlet tendrías que cambiar el nombre del fichero por applicationContext.xml.

4
  • Ahora me sale esto cada vez que intento acceder a localhost:8080/opciondefensa --->> WARN [org.springframework.web.servlet.PageNotFound] (default task-2) No mapping found for HTTP request with URI [/opciondefensa] in DispatcherServlet with name 'spring'
    – Laed
    Commented el 14 jul. 2016 a las 20:48
  • Necesitamos ver el código de tu Controlador.java que según ese error que has puesto se llama spring, donde deberías haber mapeado los servicios que vas a usar como ese "opciondefensa". Edita tu pregunta con el código. Aunque eso sería otra pregunta, el problema de no encontrar el bean definido ya lo has solucionado. Commented el 15 jul. 2016 a las 5:49
  • Ok, gracias por tu intervención.
    – Laed
    Commented el 15 jul. 2016 a las 17:51
  • Aqui dejo la nueva pregunta, gracias por al ayuda: es.stackoverflow.com/questions/16865/…
    – Laed
    Commented el 15 jul. 2016 a las 18:07

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.