Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » Spring Java Based Configuration(Configure Spring Context via Java)
Spring Java Based Configuration [message #1272387] Mon, 17 March 2014 20:15 Go to next message
Thorsten Hilker is currently offline Thorsten HilkerFriend
Messages: 31
Registered: March 2014
Member
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 #1272582 is a reply to message #1272387] Tue, 18 March 2014 08:53 Go to previous messageGo to next message
gustavo monarin is currently offline gustavo monarinFriend
Messages: 5
Registered: September 2013
Junior Member
It should work out of the box.

How are you defining your manifest imports? are you importing the spring bundles or are you importing packages?

As an annotation, if it is not available in your bundle classpath, they will just be ignored without more info.

So, first think i would check if the annotation classes that you used in your java config are available in the classpath.

I hope it hepls
Re: Spring Java Based Configuration [message #1272603 is a reply to message #1272582] Tue, 18 March 2014 10:02 Go to previous messageGo to next message
Thorsten Hilker is currently offline Thorsten HilkerFriend
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. Smile
Re: Spring Java Based Configuration [message #1272625 is a reply to message #1272603] Tue, 18 March 2014 11:05 Go to previous messageGo to next message
gustavo monarin is currently offline gustavo monarinFriend
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 Go to previous messageGo to next message
Thorsten Hilker is currently offline Thorsten HilkerFriend
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
Re: Spring Java Based Configuration [message #1273639 is a reply to message #1272637] Thu, 20 March 2014 09:50 Go to previous message
gustavo monarin is currently offline gustavo monarinFriend
Messages: 5
Registered: September 2013
Junior Member
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/
Previous Topic:struts2-virgo-plugin
Next Topic:Websocket on Virgo Tomcat Server
Goto Forum:
  


Current Time: Thu Mar 28 21:25:09 GMT 2024

Powered by FUDForum. Page generated in 0.03138 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top