Spring 3.0 M3 + Spring DM = broken javaconfig?
Hello-
We are experiencing what seems to be badly broken application context behavior in our application.
Specifically, we are using the most recent released version (1.2.0) of Spring dm (NOT dm server!) on top of a recent version of the Equinox OSGi container. We've decided to try Spring 3.0 M3. Spring 3.0 M3 has built-in support for "javaconfig"-style configuration as an alternative to defining beans in XML, via the @Configuration, @Bean, etc annotations.
What we are seeing is that when we us @Configuration-annotated configuration classes to define our beans, that our singleton beans are instead treated as prototype beans. This means that we get new instances of our singletons for each time they are referenced (!!!).
I have created sample code which demonstrates this broken behavior.
This code, when run as a regular java application, (NOT OSGi), works as expected (only one instance of Foo and Bar are created).
BUT, when the identical code is run via Spring DM, 3 different instances of Bar are created!
Note: I used a FileSystemXmlApplicationContext to load this when running as a normal java application, and put the config.xml file inside a /META-INF/spring folder when running in Spring dm.
Code:
package com.example.configtest;
public class Foo {
private Bar bar1;
private Bar bar2;
public Foo(Bar bar1, Bar bar2) {
this.bar1 = bar1;
this.bar2 = bar2;
}
public void init() {
System.out.println(toString());
}
public String toString() {
return "I am a Foo (" +
System.identityHashCode(this) +
") with bar1 = " + bar1.toString() +
" and bar2 = " + bar2.toString();
}
}
Code:
package com.example.configtest;
public class Bar {
public Bar() {
System.out.println("(..constucting a Bar)");
}
}
Code:
package com.example.configtest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public Bar bar() {
return new Bar();
}
@Bean(initMethod="init")
public Foo foo() {
return new Foo(bar(), bar());
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<bean id="config" class="com.example.configtest.Config" />
<context:component-scan base-package="com.example.configtest" />
</beans>
Console output when run as a normal java app:
Code:
(..constucting a Bar)
got Bean: I am a Foo (4824957) with bar1 = com.example.configtest.Bar@85e155 and bar2 = com.example.configtest.Bar@85e155
Console output when run as a Spring DM app:
Code:
(..constucting a Bar)
(..constucting a Bar)
(..constucting a Bar)
I am a Foo (3141854) with bar1 = com.example.configtest.Bar@6eb235 and bar2 = com.example.configtest.Bar@75b962
This seems like a pretty major incompatibility! Is anyone else using this combination of technologies (Spring 3 M3, Spring DM, @Configuration)? Should I assume that this combination is not recommended by SpringSource?
-Dave Fogel
is it okay to use a build that Failed?
Hi Chris-
It looks like Build 331 has failed, according to the Bamboo page you linked to above. Should I still attempt to use it? (I'm unfamiliar with Bamboo and what it means to fail- is it just that some tests didn't pass?)
-Dave