Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jan 2012
    Posts
    18

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •