views:

606

answers:

2

I use the following configuration for initiating my SessionFactory:

    <?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;

    <!-- Concrete services -->

    <bean id="userService" class="org.blog.service.UserService" parent="sessionService" />

    <!-- Hibernate base -->

    <bean id="sessionService" abstract="true" class="org.blog.service.SessionService">
     <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="annotatedClasses"> 
      <list> 
       <value>org.blog.domain.User</value> 
      </list> 
     </property>
     <property name="hibernateProperties">
      <props>
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
       <prop key="hibernate.show_sql">true</prop>
      </props>
     </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/website"/>
        <property name="username" value="root"/>
        <property name="password" value="enter"/>
    </bean>

</beans>

However, the following error pops up doing runtime:

    root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in URL [jndi:/localhost/Blog/WEB-INF/services.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [jndi:/localhost/Blog/WEB-INF/services.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1299)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1061)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:543)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:387)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:447)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:342)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:308)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:613)
root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [jndi:/localhost/Blog/WEB-INF/services.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1395)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1299)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1061)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:543)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:387)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:447)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:342)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:308)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:613)
root cause

java.lang.NoClassDefFoundError
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    java.lang.reflect.Constructor.newInstance(Constructor.java:501)
    org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
    org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104)
    org.springframework.orm.hibernate3.LocalSessionFactoryBean.newConfiguration(LocalSessionFactoryBean.java:771)
    org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:515)
    org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1454)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1392)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1299)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1061)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:543)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:730)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:387)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:447)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:342)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:308)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:613)

I reckon there is something wrong with my libraries.

Currently I'm using these:

  • commons-collections-3.1.jar
  • commons-dbcp.jar
  • commons-logging.jar
  • commons-pool.jar
  • dom4j-1.6.1.jar
  • ejb3-persistence.jar
  • hibernate3.jar (3.5 has included annotation)
  • javaee.jar
  • javassist-3.9.0.GA.jar
  • jstl.jar jta-1.1.jar
  • mysql-connector-java-3.1.12-bin.jar
  • org.springframework.asm-3.0.0.RC1.jar
  • org.springframework.beans-3.0.0.RC1.jar
  • org.springframework.context-3.0.0.RC1.jar
  • org.springframework.context.support-3.0.0.RC1.jar
  • org.springframework.core-3.0.0.RC1.jar
  • org.springframework.expression-3.0.0.RC1.jar
  • org.springframework.jdbc-3.0.0.RC1.jar
  • org.springframework.orm-3.0.0.RC1.jar
  • org.springframework.transaction-3.0.0.RC1.jar
  • org.springframework.web-3.0.0.RC1.jar
  • org.springframework.web.servlet-3.0.0.RC1.jar
  • slf4j-api-1.5.8.jar
  • standard.jar

Can anyone determine what is causing my errors? My source distribution is on http://code.google.com/p/jeesh-blog/

+2  A: 

You're are using SLF4J, but only have the SLF4j-API in the classpath. You also need an implementation of SLF4J, such as the SLF4j-NOP or SLF4j-Simplelogger.

You can see this by implementing a simple test case, which initializes the Spring ApplicationContext, and run it with Java 6 to see which Class cannot be found:

public class AppCtxTest {
 @Test
 public void testApplicationContext() throws Exception {
  FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(
    "app/WEB-INF/services.xml");
  ctx.close();
 }
}

The output will show what is missing;

org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'userService' defined in file [C:\Users\mhaller\workspace\Blog\app\WEB-INF\services.xml]:
 Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory';
 nested exception is ...
Caused by: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'sessionFactory' defined in file [C:\Users\mhaller\workspace\Blog\app\WEB-INF\services.xml]:
 Invocation of init method failed;
 nested exception is java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
Caused by: java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder

Indeed there are some more dependencies missing:

  • SLFJ4j Implementation (slf4j-xxx.jar)
  • Java Persistence API (jpa.jar): javax.persistence.ElementCollection
mhaller
Where exactly can I find this jpa.jar, cannot find anything related in my GlassFish SDK lib folder or on Google.
Jeroen
A: 

hibernate javax/persistence/ElementCollection

*JPA 2.0 being shipped as part of the Java EE 6 release (Sept09)

*JPA 2.0 Reference Implementation will be EclipseLink project(open source TopLink) Shipped with WLS, Glassfish, Spring, or standalone http:/ www.eclipse.org/eclipselink

Download JPA 2.0 Proposed Final Draft and have a look http:/ / www.jcp.org/ en/ jsr/ detail?id= 317

Vovka