Sorry if this is obvious- I'm new to JavaConfig:
If I have a config class something like this:
@Configuration
public class Config {
@Bean
public Foo foo() {
return new Foo(...);
}
}
and I create and use my context like this:
JavaConfigApplicationContext context =
new JavaConfigApplicationContext(Config.class);
Foo foo = context.getBean(Foo.class);
What is the best way to _programatically_ add in a (singleton) object that the Foo constructor depends on? (Something that can't be created/defined in the Config class- it's provided to my code at the time I set up my context).
I assume I need to do something like this in my Config code:
@Configuration
public class Config extends ConfigurationSupport {
@Bean
public Foo foo() {
return new Foo( this.getBean(Bar.class) );
}
}
and then tried doing something like:
JavaConfigApplicationContext context =
new JavaConfigApplicationContext(Config.class);
context.getBeanFactory().registerSingleton("bar", bar);
Foo foo = context.getBean(Foo.class);
But this didn't work. Do I have to create a parent context for the javaconfig context? Is there no other way to register an object so that my Configuration class code can access it?
Thanks,
-Dave Fogel


