Announcement Announcement Module
Collapse
No announcement yet.
Can we dynamically add Bean into Spring AnnotationConfigApplicationContext? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can we dynamically add Bean into Spring AnnotationConfigApplicationContext?

    Hi,

    can we dynamically add one existing bean into spring AnnotationConfigApplicationContext when we initialize the context?

    e.g. I have this spring java config:
    Code:
    @Configuration
    public class MyConfig {
    
      @Autowire public MyBean mybean;
    
      @Bean public AnotherBean another() {
         return new AnotherBean( mybean );
      }
    }
    Then, when I initialize AnnotationConfigApplicationContext, I want to dynamically add MyBean instance into context, e.g.
    Code:
        MyBean mybean = createMyBean....
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()
        //context.add(mybean )  // so MyConfig below can autowire mybean instance
        context.register(classOf[MyConfig ])
    Could you please let me know how I can add mybean instance to AnnotationConfigApplicationContext?

    David
Working...
X