@Component(value)
属性主要用于为类显式指定 Bean 的名称,而 @Qualifier
注解主要用于在注入时指定具体的 Bean 名称,两者解决的都是依赖注入时歧义的问题。
那么如何理解上述的不同之处呢?我们看如下的程序
一、@Qualifier注解
@Qualifier一般和@Autowired一起使用,目的是为了指定注入某个已存在的Bean对象,不能在ElementType.TYPE的Target上使用。
Springconfig:
@Configuration
@ComponentScan("org.example")
@PropertySource("classpath:jdbc.properties")
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class SpringConfig {
@Bean
@Qualifier("UTC") // 指定注入名称为"z"的ZoneId
ZoneId createZoneOfUTC8() {
return ZoneId.of("UTC+08:00");
}
@Bean
@Qualifier("GMT")
ZoneId createZoneOfGMT() {
return ZoneId.of("GMT");
}
@Bean
public Logger getLogger(){
return Logger.getLogger(SpringConfig.class);
}
}
MailService:
@Component
public class MailService {
@Autowired(required = false)
@Qualifier("UTC")
private ZoneId zoneId;
public void setZoneId(ZoneId zoneId) {
this.zoneId = zoneId;
}
public String getTime() {
return ZonedDateTime.now(this.zoneId).format(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
@MetricTime("sendLoginMail")
public void sendLoginMail(@NotNull User user) {
System.err.printf("Hi, %s! You are logged in at %s%n", user.getName(), getTime());
}
@MetricTime("sendRegistrationMail")
public void sendRegistrationMail(@NotNull User user) {
System.err.printf("Welcome, %s!%n", user.getName());
}
}
从上面的程序可以看到,这是一个config文件,其中有两个ZoneId的bean,但是我们知道Spring的IOC容器在创建Bean时是Singleton模式的,所以如果不指定注入的别名,注入的时候就会报错(尝试把MailService中的ZoneId的对象上Qualifier注解给注释掉,会报出以下错误):
Could not autowire. There is more than one bean of 'ZoneId' type.
Beans:
createZoneOfGMT (SpringConfig.java) createZoneOfUTC8 (SpringConfig.java)
二、@Component
注解
注:Component和Repository、Service都可以互换,用来指定该类是作Service还是Dao,为了指定一个实现类的别名,只能在ElementType.TYPE的Target上使用。
我有两个AccountService接口的实现类,但是IOC容器在装配实现类的时候是两个Bean都装配的,调用的时候要根据他们的别名进行调用。
@Service("accountServiceImpl")
public class AccountServiceImpl implements AccountService {
@Autowired
AccountDao accountDao;
@Override
public void transfer(User out, User in, double money) throws RuntimeException, IOException {
accountDao.outMoney(out,money);
// int a=1/0;
accountDao.inMoney(in,money);
out.balance -=money;
in.balance +=money;
}
}
@Service("accountServiceImpl2")
public class AccountServiceImpl2 implements AccountService {
@Autowired
AccountDao accountDao;
@Override
public void transfer(User out, User in, double money) throws RuntimeException, IOException {
System.err.println("accountServiceImpl2,I can't do anything");
}
}
public class App
{
private static final Logger logger = Logger.getLogger(App.class);
public static void main( String[] args ) throws Exception {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = applicationContext.getBean(UserService.class);
List<User> users = userService.getAllUsers();
AccountService accountService = (AccountService) applicationContext.getBean("accountServiceImpl");
accountService.transfer(users.get(1),users.get(0),2000.0);
userService.login("bob@example.com","oldpassword");
}
}
分别注入两个不同的Bean时transfer的效果:
accountServiceImpl2:
[转账开始]:转出用户:Alice,待转出金额:2000.00,目前金额:34795.92 [转账开始]:转入用户:Bob,待转入金额:2000.00,目前金额:60000.00
accountServiceImpl2,I can't do anything
[转账成功]:转出用户:Alice,已转出金额:2000.00,剩余金额:34795.92 [转账成功]:转入用户:Bob,已转入金额:2000.00,剩余金额:60000.00
accountServiceImpl:
[转账开始]:转出用户:Alice,待转出金额:2000.00,目前金额:34795.92 [转账开始]:转入用户:Bob,待转入金额:2000.00,目前金额:60000.00
[转账成功]:转出用户:Alice,已转出金额:2000.00,剩余金额:32795.92 [转账成功]:转入用户:Bob,已转入金额:2000.00,剩余金额:62000.00
如果不指定两个AccountService实现类的别名话,会报错,具体可以自己尝试一下,(就是把service内的value属性的值去掉)
三、总结
@Component(value)
属性主要用于为(自定义)类显式指定 Bean 的名称,也只能用于类、接口和枚举等类型,用于IOC容器创建和调用,而 @Qualifier
注解主要用于在注入指定的Bean,一般和autowried一起使用,多用于字段、参数和方法上,两者解决的都是依赖注入时歧义的问题。