Is it possible to set a bean name using annotations in Spring Framework?

后端 未结 1 1455
失恋的感觉
失恋的感觉 2020-12-25 12:40

I have a bean like this:

@Bean
public String myBean(){
    return \"My bean\";
}

I want to autowire it:

@Autowired
@Qualifi         


        
相关标签:
1条回答
  • 2020-12-25 13:13

    What you are asking is already available in Spring 4.3.3

    By default, configuration classes use a @Bean method’s name as the name of the resulting bean. This functionality can be overridden, however, with the name attribute.

    @Configuration
    public class AppConfig {
    
        @Bean(name = "myFoo")
        public Foo foo() {
            return new Foo();
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题