Home » Eclipse Projects » Virgo » Spring Java Based Configuration(Configure Spring Context via Java)
Spring Java Based Configuration [message #1272387] |
Mon, 17 March 2014 16:15  |
Eclipse User |
|
|
|
Hi!
I've some problems configuring a Spring Application Context based on Java.
Configuration via XML is no problem.
Don't won't to go in details now, maybe later, if you want to have a detailed description.
My question is simple:
Is there a standard mechanism to use "Spring Java Based Configuration" in Virgo?
Regards,
Thorsten
PS If you need more infos please ask. But maybe the answer is simple.
|
|
| |
Re: Spring Java Based Configuration [message #1272603 is a reply to message #1272582] |
Tue, 18 March 2014 06:02   |
Eclipse User |
|
|
|
Hi!
To clarify things a little bit here is what I did:
1. The Bean Classes:
package de.hilker.virgotest;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Testbean implements InitializingBean {
@Autowired
private Testbean2 testbean2;
public Testbean() {
System.out.println("CTOR Testbean");
}
public Testbean2 getTestbean2() {
return testbean2;
}
public void setTestbean2(Testbean2 testbean2) {
this.testbean2 = testbean2;
}
@Override
public void afterPropertiesSet() throws Exception {
testbean2.alive();
}
}
package de.hilker.virgotest;
import org.springframework.stereotype.Component;
@Component
public class Testbean2 {
public Testbean2() {
System.out.println("CTOR Testbean2");
}
public void alive() {
System.out.println("Testbean2: I'm alive!");
}
}
2. The Application Context XML in META-INF/spring:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<context:annotation-config />
<bean id="testbean" class="de.hilker.virgotest.Testbean" />
<bean id="testbean2" class="de.hilker.virgotest.Testbean2" />
</beans>
3. The Manifest:
Manifest-Version: 1.0
Export-Package: de.hilker.virgotest;uses:="org.springframework.beans.f
actory,org.springframework.beans.factory.annotation,org.springframewo
rk.stereotype"
Bundle-Version: 1.0.0
Tool: Bundlor 1.1.2.RELEASE
Bundle-Name: Virgotest
Bundle-ManifestVersion: 2
Import-Package: org.springframework.beans.factory,org.springframework.
beans.factory.annotation,org.springframework.stereotype
Bundle-SymbolicName: de.hilker.virgotest
OK, everything runs smoothly. The output is like expected:
[2014-03-18 09:31:32.494] TCP Connection(10)-127.0.0.1 <DE0000I> Installing bundle 'de.hilker.virgotest' version '1.0.0'.
[2014-03-18 09:31:32.509] TCP Connection(10)-127.0.0.1 <DE0001I> Installed bundle 'de.hilker.virgotest' version '1.0.0'.
[2014-03-18 09:31:32.514] TCP Connection(10)-127.0.0.1 <DE0004I> Starting bundle 'de.hilker.virgotest' version '1.0.0'.
CTOR Testbean
CTOR Testbean2
Testbean2: I'm alive!
[2014-03-18 09:31:32.587] start-signalling-1 <DE0005I> Started bundle 'de.hilker.virgotest' version '1.0.0'.
Now the Java Based appoach:
The context is now build in this Class (instead oft the XML):
package de.hilker.virgotest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public Testbean testbean() {
return new Testbean();
}
@Bean
public Testbean2 testbean2() {
return new Testbean2();
}
}
The XML changed to:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<context:component-scan base-package="de.hilker.virgotest" />
</beans>
After redeploy to Virgo, I got this message:
...
Caused by: java.lang.ClassNotFoundException: org.springframework.cglib.core.ReflectUtils
...
Ok, added the org.springframework.core-bundle to the manifest:
Manifest-Version: 1.0
Export-Package: de.hilker.virgotest;uses:="org.springframework.beans.f
actory,org.springframework.beans.factory.annotation,org.springframewo
rk.context.annotation,org.springframework.stereotype"
Bundle-Version: 1.0.0
Tool: Bundlor 1.1.2.RELEASE
Bundle-Name: Virgotest
Bundle-ManifestVersion: 2
Import-Package: org.springframework.beans.factory,org.springframework.
beans.factory.annotation,org.springframework.context.annotation,org.s
pringframework.stereotype
Bundle-SymbolicName: de.hilker.virgotest
Import-Bundle: org.springframework.core;version="[3.2.5.RELEASE,3.2.5.RELEASE]"
Everything runs smoothly again!
(Strange, that I've to import the Spring Core. Why doesn't the "XML"-approach need the bundle?)
Do I have to import this Spring-Bundle and why?
Is there a better elegant way?
Thanks in advance!
Thorsten
PS For me the most elegant way would be to be able to put the Config.class into the META-INF/spring folder, and Virgo does the rest.
|
|
| |
Re: Spring Java Based Configuration [message #1272637 is a reply to message #1272625] |
Tue, 18 March 2014 07:35   |
Eclipse User |
|
|
|
Hi Gustavo,
you are right, it's also possible, to import the packages instead of the whole bundle.
I tried it:
Manifest-Version: 1.0
Export-Package: de.hilker.virgotest;uses:="org.apache.commons.io.input
,org.springframework.beans.factory,org.springframework.beans.factory.
annotation,org.springframework.context.annotation,org.springframework
.stereotype"
Bundle-Version: 1.0.0
Tool: Bundlor 1.1.2.RELEASE
Bundle-Name: Virgotest
Bundle-ManifestVersion: 2
Import-Package: org.apache.commons.io.input,
org.springframework.beans.factory;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.beans.factory.annotation;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.cglib.core;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.cglib.proxy;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.cglib.reflect;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.context.annotation;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.stereotype;version="[3.2.5.RELEASE,3.2.5.RELEASE]"
Bundle-SymbolicName: de.hilker.virgotest
The packages are org.springframework.cglib.core;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.cglib.proxy;version="[3.2.5.RELEASE,3.2.5.RELEASE]",
org.springframework.cglib.reflect;version="[3.2.5.RELEASE,3.2.5.RELEASE]"
But this was not my original question.
I was wondering, why Virgo needs this three packages or the bundle.
I do not use any component from this packages explicitly.
(So the eclipse bundler, building the manifest automatically, is not noticing them)
Why do I need to import stuff, which is used indirectly by Spring?
And why does the configuration via XML does not need the packages/bundle?
Greetings,
Thorsten
|
|
|
Re: Spring Java Based Configuration [message #1273639 is a reply to message #1272637] |
Thu, 20 March 2014 05:50  |
Eclipse User |
|
|
|
Hi Thorsten,
Great that it is working.
Why do I need to import stuff, which is used indirectly by Spring?
This is an interesting question. The short answer could be that there is a problem in the spring manifest where it does not import by itself these packages. The long answer and a bit of guess based on a bit of my experience with spring is that in other areas than spring-java-config, they try to make the use of cglib optional, what give us users a big flexibility and compatibility with different jvms versions. This other topic, one says that the java-config mechanism relies on cglib[1].
[1]http://www.eclipse.org/forums/index.php/t/488617/
|
|
|
Goto Forum:
Current Time: Mon May 12 03:00:05 EDT 2025
Powered by FUDForum. Page generated in 0.05546 seconds
|