Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding dynamic beans to spring web context

Tags:

java

spring

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.

like image 794
Frank Orellana Avatar asked Sep 06 '12 05:09

Frank Orellana


People also ask

How do I register a bean in Spring context?

You must call registerBeanDefinition on the bean definition registry to register a new bean definition. This registerBeanDefinition takes the bean name and definition as input. You can use the BeanDefinitionBuilder to create a BeanDefinition programmatically (introduced in Spring 2.0). bdr.

Who dynamically wires up beans and settings and apply them to your application context?

Instead, when you start up your application, Spring Boot dynamically wires up beans and settings and applies them to your application context. Building applications with Spring Boot is really fast, especially for web applications.

How do you pass parameters dynamically to Spring beans?

String hello(String name) { return "Hello, " + name; } } // and later in your application AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration. class); String helloCat = (String) context. getBean("hello", "Cat"); String helloDog = (String) context.


1 Answers

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:

  • http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-extension-factorybean
  • http://blog.springsource.org/2011/08/09/whats-a-factorybean/
like image 150
pap Avatar answered Oct 04 '22 01:10

pap