Home » Eclipse Projects » Virgo » Spring Java Based Configuration(Configure Spring Context via Java)
| |
Re: Spring Java Based Configuration [message #1272603 is a reply to message #1272582] |
Tue, 18 March 2014 10:02 |
Thorsten Hilker Messages: 31 Registered: March 2014 |
Member |
|
|
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 #1272625 is a reply to message #1272603] |
Tue, 18 March 2014 11:05 |
gustavo monarin Messages: 5 Registered: September 2013 |
Junior Member |
|
|
Hi Thorsten,
You don;t "have to" import the spring core as a bundle. You just have to assure that, independent of your strategy(import bundle or packages), all the classes that you refer in your code are in imported.
I would suggest you to have a quick search about import bundle vs import packages. Also research how to make sure that the imports will be synchronized with further development( there are tools like bnd, bundlor among others that automate the manifest generation for you.)
So the question about the why xml approach works is that it will create the xml defined beans even if it does not find the @Component or @Configuration in your classes. Also you don;t need to annotate @Component in your beans that you are defining in your xml or java config.
I didn't have the opportunity yet to play with java confing on osgi environments. The java config inside the META-INF/spring sounds nice, but i would keep the meta inf closer to the osgi specification and from there bootstrap our spring config from our source classpath.
Regards
[Updated on: Tue, 18 March 2014 11:05] Report message to a moderator
|
|
|
Re: Spring Java Based Configuration [message #1272637 is a reply to message #1272625] |
Tue, 18 March 2014 11:35 |
Thorsten Hilker Messages: 31 Registered: March 2014 |
Member |
|
|
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
|
|
| |
Goto Forum:
Current Time: Tue Sep 24 09:30:07 GMT 2024
Powered by FUDForum. Page generated in 0.04820 seconds
|