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?
get OSGi service for Dictionary and assign to bean A
create bean directly and assing to bean A
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
Joined: May 24, 2007
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 unittesting (loading mock objects, etc).
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)