javaspringspring-bootspring-bean

How can I create two beans of same class in a Spring Context?


I can declare a bean in Spring using @Bean annotation. Let's say I declare two beans of String type in my Application Context.

@Bean
public String country(){ return "India";}

@Bean
public String continent(){ return "Asia";}

In this case what will happen when the Spring Container will boot strap? Would there be any error?


Solution

  • Two spring beans can be created in same context. But they need to be created in separate @Configuration files and separate access.