I have these beans:
<bean id="Child" class="test.Child"></bean>
<bean id="Parent" class="test.Parent">
<property name="child" ref="Child" />
</bean>
I use Axis2, Spring with Hibernate on Tomcat. The Axis services are located in the Parent bean. It is mapped as SpringBeanName in the services.xml, including correct ServiceClass. I placed logs in the get and set methods for the Child beans variables inside the Parent bean:
private Child child;
public void setChild(Child child) {
this.child = child;
System.out.println(child);
}
public Child getChild() {
System.out.println(child);
return child;
}
I have a Spring initializing service called at startup, to load Spring in the startUp method in a service implementing ServiceLifeCycle.
ClassLoader classLoader = service.getClassLoader();
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:spring/applicationContext.xml" }, false);
applicationContext.setClassLoader(classLoader);
applicationContext.refresh();
This is a simple example project, since the logic is quite more complex in real. Anyway, the result is:
The problem is when Tomcat get restarted. In the starting logs, the set method is called with non null object. But when the service is called - Null Pointer Exception, child is null. Redeploying the AAR makes everything working again. But after a new restart - the child is null again.
What is happening? Every help will be appreciated.
Note: I do not create the child with new. All scopes of the beans are default. I tried with prototype scope with no success. Session and request scopes threw an exception that these scopes are not recognized:
No Scope registered for scope '...'
Note 2: Uncommenting the tag in the context.xml in the conf folder did not changed anything. I restarted Tomcat after the change.
I found a solution, though, it does not cover all the range of situations. Changing the scope of the service in the services.xml
from application
to request
(or the other two) fixes the problem which seems a bit logical.
Note: In case that application
scope is what you really need, this fix is not applicable.