[英]Is it possible to set a bean name using annotations in Spring Framework?
I have a bean like this:我有一个这样的豆子:
@Bean
public String myBean(){
return "My bean";
}
I want to autowire it:我想自动装配它:
@Autowired
@Qualifier("myBean")
public void setMyBean(String myBean){
this.myBean=myBean;
}
I need something like:我需要类似的东西:
@Bean(name="myCustomBean")
Is it possible to use custom names names for beans out of the box?是否可以开箱即用地为 bean 使用自定义名称? If it isn't possible out of the box then how to create such a bean?
如果无法开箱即用,那么如何创建这样的 bean?
What you are asking is already available in Spring reference您要问的内容已在 Spring 参考中可用
By default, configuration classes use a @Bean method's name as the name of the resulting bean.
默认情况下,配置类使用@Bean 方法的名称作为结果 bean 的名称。 This functionality can be overridden, however, with the name attribute.
但是,可以使用 name 属性覆盖此功能。
@Configuration
public class AppConfig {
@Bean(name = "myFoo")
public Foo foo() {
return new Foo();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.