Announcement Announcement Module
Collapse
No announcement yet.
How to instantiate beans of same type with different bean ids using JavaConfig ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to instantiate beans of same type with different bean ids using JavaConfig ?

    In the simple case for one bean definition in XML would be say :

    <beans>
    <bean id="helloWorld" class="com.blah.HelloWorld" />
    </beans>

    The equivalent of this in JavaConfig would be:

    package com.blah;
    import org.springframework.context.annotation.*;

    @Configuration
    public class HelloWorldConfig {

    @Bean
    public HelloWorld helloWorld(){
    return new HelloWorld();
    }
    }


    So extending this to more than one bean

    <beans>
    <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" />
    <bean id="helloWorldAgain" class="com.tutorialspoint.HelloWorld" />
    </beans>

    What would be equivalent JavaConfig to this?

  • #2
    Please use [ codce][/code ] tags when posting code...

    SImply define 2 beans, the method name is the bean name (or specify the name in the Bean annotation).

    Comment

    Working...
    X