aspose file tools*
The moose likes Spring and the fly likes Portability of Spring applications running in OSGi and outside of OSGi Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Portability of Spring applications running in OSGi and outside of OSGi" Watch "Portability of Spring applications running in OSGi and outside of OSGi" New topic
Author

Portability of Spring applications running in OSGi and outside of OSGi

Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
Maybe more of an OSGi question, but it is in a Spring context.

I have some apps that create all their bean via spring. However, when they run on OSGi, some of the beans I get by using Spring OSGi Service lookups. For example;

<osgi:reference id="dictionaryBean"
context-class-loader="service-provider"
interface="com.foobar.Dictionary"/>

So then I have a dictionary bean I can use elsewhere (if there's a bundle installed which implements the service).

But if I run this app, OUTSIDE osgi, then this call fails because I'm not running in OSGi. What I would like to do is either detect I'm in OSGi, or on error catch an example in the Spring XML and then create my own dictionary bean directly.

Is there some type of conditional syntax in Spring I can use for this?

try
get OSGi service for Dictionary and assign to bean A
catch
create bean directly and assing to bean A

Josh Long
author
Greenhorn

Joined: Apr 04, 2011
Posts: 24

Spring 3.1 offers profiles which lets you conditionally enable certain beans:



the idea is that a bean could be 'active' in one environment, but disabled in another. The environment's arbitrary, of course. It might be that you need to use <jee:jndi-factory/> in production and a regular HSQL DataSource in your local environment, and this would help with that sort of situation, too. For more, see http://blog.springsource.com/2011/02/14/spring-3-1-m1-introducing-profile/


Thanks, Josh Long
Spring Developer Advocate, SpringSource, a division of VMware
http://blog.springsource.org - http://www.joshlong.com
Augusto Sellhorn
Ranch Hand

Joined: May 24, 2007
Posts: 57
Thanks, I think that's exactly what I need. Read your blog post kind of quick, but it was unclear to me how to activate a profile, your examples showed doing it via a spring code before loading the spring/app context. We start our Spring apps from XML, so I'm wondering how to set the active profile in this manner.

For my OSGi example, I want to run a profile if something detects that it is running in OSGi (what does that is another question). But I was hoping it could be done mostly from XML.

Either way this looks like a nice feature, and it will solve our issues we have with unit testing (loading mock objects, etc).
Josh Long
author
Greenhorn

Joined: Apr 04, 2011
Posts: 24

Hi, you have several options: use a cmd line switch, set it explicitly on application context before starting it, and - in environments like web application contexts where the servlet container creates the AC and thus you don't have a direct reference, use an ApplicationContextInitializer to get an opportunity to set the active profiles on an AC before its initialized. This is also ideal for dynamically testing which environment you're in and setting the profile accordingly. See that and other blogs around same time, as well as 3.1 m1 docs for details (I'm on my phone)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Portability of Spring applications running in OSGi and outside of OSGi
 
Similar Threads
OSGi ain't the only way to modularize your apps
Calling Osgi service from Servlet
Spring DM versus JSR-291
OSGi + Glassfish + JEE 6 howto?
Spring Dm : Export service dynamicaly