在春天使用@Bean和@Component的名称和别名

时间:2016-10-07 11:51:06

标签: spring

我们如何定义

a)单个名称
b)多个名称(别名)

使用@Bean和@Component注释?

在XML中,我们定义如下:

单个名称:

<bean name="accountService" autowire="constructor" class="com.wiley.beginningspring.ch2.AccountServiceImpl" autowire-candidate="false">
</bean>

对于别名:

<bean name="accountService,a,b" autowire="constructor" class="com.wiley.beginningspring.ch2.AccountServiceImpl" autowire-candidate="false">
</bean>

但是@Bean和@Component(或@Service,@ Repository)呢?

2 个答案:

答案 0 :(得分:2)

这仍是功能请求https://jira.spring.io/browse/SPR-6736

除了@Bean

@Configuration
public class Config {

  @Bean(name = { "accountService", "acc" })
  public AccountServiceImpl cccountServiceImpl() {
    return new AccountServiceImpl();
  }

}

答案 1 :(得分:0)

bean名称接受字符串数组,因此可行。 @Bean({"b1", "b2"}

相关问题