File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Portability of Spring applications running in OSGi and outside of OSGi

 
Augusto Sellhorn
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Linux Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
Augusto Sellhorn
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
Linux Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic