我有一个这样的 bean :
@Bean
public String myBean(){
return "My bean";
}
我想自动接线:
@Autowired
@Qualifier("myBean")
public void setMyBean(String myBean){
this.myBean=myBean;
}
我需要类似的东西:
@Bean(name="myCustomBean")
是否可以开箱即用地为 bean 使用自定义名称?如果不能开箱即用,那么如何创建这样的 bean?
最佳答案
您要问的是已经在 Spring 推出 reference
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();
}
}
关于java - 是否可以在 Spring Framework 中使用注释设置 bean 名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39898028/