0
votes

I've searched internet and debugged for a couple of hours but i'm stuck on finding whats wrong. I have the follwng webapplication setup: Jetty 8.1.0.v20120127 Spring-orm 3.1.2.RELEASE Spring-web 3.1.2.RELEASE Hibernate-core 4.1.7-final

In web.xml defined via sessionFactoryBeanName init-param: hibernateSessionFactory

While application starts no errors and shows the following:

XmlWebApplicationContext: loads a few beans from applicationContext.xml ClassPathXmlApplicationContext: loads a few beans from applicationContext-hibernate.xml (also hibernateSessionFactory)

When loading a page (request) OpenSessionInViewFilter gets active and gives the following:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hibernateSessionFactory' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1102)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:156)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:141)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1336)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:172)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:267)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1336)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:483)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:521)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:233)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1065)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:412)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:192)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:999)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:250)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:149)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
at org.eclipse.jetty.server.Server.handle(Server.java:351)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:451)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:916)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:76)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:45)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:534)
at java.lang.Thread.run(Thread.java:722)

While debugging on DefaultListableBeanFactory.java:553 the beanDefinitionMap shows a keyset with only the beans loaded with XmlWebApplicationContext. Thus none of the beans from the file applicationContext-hibernate.xml are shown.

bean definition (A break point on one of the setters of LocalSessionFactoryBean shows me that the bean is created):

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocations" ref="hibernateConfigLocations"/>
<property name="dataSource" ref="hibernateDatasource"/>
<property name="namingStrategy" ref="hibernateNamingStrategy"/>
<property name="hibernateProperties" ref="hibernateProperties"/>

I was expecting to find all beans from both XmlWebApplicationContext and the ClassPathXmlApplicationContext. Can anybody tell what is causing my problem and please let me know when missing any information on finding the problem?

web.xml:

<display-name>webshop-cms</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<context-param>
    <param-name>configuration</param-name>
    <param-value>deployment</param-value>   
</context-param>

<filter>
    <filter-name>webshop-cms</filter-name>
    <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
    <init-param>
        <param-name>applicationClassName</param-name>
        <param-value>nl.name.webshop.cms.CmsApplication</param-value>
    </init-param>
    <init-param>
        <param-name>ignorePaths</param-name>
        <param-value>/assets</param-value>
    </init-param>
</filter>

<filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>sessionFactoryBeanName</param-name>
        <param-value>hibernateSessionFactory</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>webshop-cms</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
    <filter-name>openSessionInView</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

applicationContext.xml:

<context:component-scan base-package="nl.name.webshop.model" />
<context:component-scan base-package="nl.name.webshop.service" />
<context:component-scan base-package="nl.name.webshop.dao" />

<bean id="webshop.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
        <list>
            <value>classpath:applicationContext-hibernate.xml</value>
        </list>
    </constructor-arg>
</bean>

applicationContext-hibernate.xml:

<bean id="hibernateNamingStrategy" class="org.hibernate.cfg.DefaultComponentSafeNamingStrategy" />

<bean id="hibernateConfigLocations" class="java.util.ArrayList">
    <constructor-arg>
        <list>
            <value>classpath:hibernate.cfg.xml</value>
        </list>
    </constructor-arg>
</bean> 

<bean id="jndiDatasourceName" class="java.lang.String">
    <constructor-arg value="java:comp/env/jdbc/webshop"/>
</bean>

<jee:jndi-lookup id="hibernateConfig" jndi-name="jdbc/webshop-hibernate-config"/>
<jee:jndi-lookup id="hibernateDatasource" jndi-name="jdbc/webshop-datasource"/>

<bean id="hibernatePropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="hibernateConfig"/>
</bean>

<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="properties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        </props>
    </property>
</bean>

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocations" ref="hibernateConfigLocations"/>
    <property name="dataSource" ref="hibernateDatasource"/>
    <property name="namingStrategy" ref="hibernateNamingStrategy"/>
    <property name="hibernateProperties" ref="hibernateProperties"/>
</bean>

1
Can you show us your configuration for loading the spring context files? - JamesB
Sorry it's my first post and just can't get the code blocks right.. I'll try again. - user1947973
Oké added the files.. hopefully it's clear de start xml tags are gone also the last close tag per file i think - user1947973

1 Answers

0
votes

Shouldn't the definition of your bean be:

<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

i.e. use id attribute instead of name.

[Edit] Actually, is there a reason to load the applicationContext-hibernate.xml file this way:

<bean id="webshop.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
        <list>
            <value>classpath:applicationContext-hibernate.xml</value>
        </list>
    </constructor-arg>
</bean>

I think you could simply have the following in applicationContext.xml:

<import resource="/path/relative/to/application/context/applicationContext-hibernate.xml"/>