7

I need to be able to add new beans to a spring web context (in any scope) for classes that might and might not be defined in the classpath at compilation time.

For example, I could create some of this classes dynamically and then register a singleton or session bean for this class.

I read a little about the BeanFactoryPostProcessor, but not sure if it will work on a web context, and if I understand correctly it will only work before actually loading the bean instances and not after that, or am I wrong?

I haven't been able to find information on how to do this in a AnnotationConfigWebApplicationContext, and at least in my test all the beans I create dinamically are not injected into other instances, and even if they were I only see the method to register Singleton beans but not for other scopes:

ctx.getBeanFactory().registerSingleton("dummy", dummy);

P.D.

I found this question : Dynamic creation of beans in Spring but when I try to add any bean, bean definition or bean definition builder to the web context like this the bean does not get loaded:

AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.setServletContext(servletContext);
ctx.refresh();
//
log.trace("Registering first test bean");
Test test = new Test();
test.setDummy("x1");
ctx.getBeanFactory().registerSingleton("myTestBean1", test);        

log.trace("Registering second test bean");
BeanDefinition testDef = new RootBeanDefinition(Test.class);        
testDef.setScope(BeanDefinition.SCOPE_SINGLETON);        
ctx.getBeanFactory().registerSingleton("myTestBean2", testDef);

Test dummy1 = (Test)ctx.getBean("myTestBean1");
Object dummy2 = ctx.getBean("myTestBean2");

log.trace("beans: 1: {}; 2: {}", dummy1, dummy2); //<--this works, but...
ctx.refresh();

ctx.register(MyConfig.class); //configuring other beans

...When I do this from another bean configured inside MyConfig.class or annotated with @Component (It might also even be another dynamic component):

@Autowire Test myTestBean1;
//Or this:
Object a = this.ctx.getBean("myTestBean1"); //Or myTestBean2

It throws a NoSuchBeanDefinitionException

Please help me!! I been looking around a lot and havent found any useful info, or maybe I have but I cannot test all what I find in the web in such short time and the spring documentation only seems to be very good for normal cases and xml configurations, not for this crazy things I'm trying to do and not for programatically configuration.

1
  • I wonder if explicitly handling object lifecycles in Java code wouldn't make things simpler Commented Sep 6, 2012 at 14:25

1 Answer 1

3

What you're after can't be accomplished, sorry. Autowiring happens when a bean is initially created. In your case, you expect to wire a bean before you have added it to the factory, which of course won't work - Spring doesn't do time-travel.

What I think you are after is the factory pattern. In Spring, you can defer instantiation of a bean to a factory you create by implementing the FactoryBean. Read more here:

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but in the example I create the bean before Autowiring it in another bean, I edited the question to be more clear, It looks interesting though... gonna take a look at this in case I need another approach
@Lando In your example, you start by calling ctx.refresh() on line two, which builds the entire bean factory and processes all autowirings. That happens before you do anything else. Regardless, manually inserting beans at runtime is not how Spring is meant to work. I'm sure you can get it to function somehow, but it's not going to be pretty. If you need to delay/defer bean creation you should work with scopes and/or factories and interfaces.
If I don't call refresh before, I get an IllegalStateException, i check the code and it validates that the factory is not null when registering new singletons. The problem with factories is that I would still need to have the name of the bean defined so spring delegates the creation to the factory. I'm running out of ideas :(
@Lando Don't work against the framework, work with it. You are trying to do something that is contrary to the intention and design of Spring. Take an extra moment and think about the lifecycle of each of your objects and what you are trying to do and you'll see that the reason why you are running out of ideas is because your basic supposition is backwards.
The correct blog post link appears to be spring.io/blog/2011/08/09/what-s-a-factorybean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.