• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Portability of Spring applications running in OSGi and outside of OSGi

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
author
Posts: 24
5
Spring Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 X Long
author
Posts: 24
5
Spring Tomcat Server Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
His brain is the size of a cherry pit! About the size of this ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic