Spring @Autowired 是按名称还是按类型注入 bean?
Does Spring @Autowired inject beans by name or by type?
我正在阅读开始 spring (wiley press) 的书。在第 2 章中有一个例子
关于 Java 配置和 @Autowired
。它提供了这个 @Configuration
class
@Configuration
public class Ch2BeanConfiguration {
@Bean
public AccountService accountService() {
AccountServiceImpl bean = new AccountServiceImpl();
return bean;
}
@Bean
public AccountDao accountDao() {
AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
//depedencies of accountDao bean will be injected here...
return bean;
}
@Bean
public AccountDao accountDaoJdbc() {
AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();
return bean;
}
}
和这个普通 bean class
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
...
}
当我 运行 代码时,它起作用了。但我预计会出现异常,因为我在配置中定义了 2 个具有相同类型的 bean。
我意识到它是这样工作的:
- 如果Spring遇到多个具有相同类型的bean,它会检查字段名称。
- 如果找到具有目标字段名称的 bean,它会将那个 bean 注入到该字段中。
这不是错了吗? Spring 对 Java 配置的处理是否存在错误?
documentation 对此进行了解释
For a fallback match, the bean name is considered a default qualifier
value. Thus you can define the bean with an id "main" instead of the
nested qualifier element, leading to the same matching result.
However, although you can use this convention to refer to specific
beans by name, @Autowired
is fundamentally about type-driven injection
with optional semantic qualifiers. This means that qualifier values,
even with the bean name fallback, always have narrowing semantics
within the set of type matches; they do not semantically express a
reference to a unique bean id
所以,不,这不是错误,这是预期的行为。如果按类型自动装配找不到单个匹配的 bean,bean id(名称)将用作后备。
我正在阅读开始 spring (wiley press) 的书。在第 2 章中有一个例子
关于 Java 配置和 @Autowired
。它提供了这个 @Configuration
class
@Configuration
public class Ch2BeanConfiguration {
@Bean
public AccountService accountService() {
AccountServiceImpl bean = new AccountServiceImpl();
return bean;
}
@Bean
public AccountDao accountDao() {
AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
//depedencies of accountDao bean will be injected here...
return bean;
}
@Bean
public AccountDao accountDaoJdbc() {
AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();
return bean;
}
}
和这个普通 bean class
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
...
}
当我 运行 代码时,它起作用了。但我预计会出现异常,因为我在配置中定义了 2 个具有相同类型的 bean。
我意识到它是这样工作的:
- 如果Spring遇到多个具有相同类型的bean,它会检查字段名称。
- 如果找到具有目标字段名称的 bean,它会将那个 bean 注入到该字段中。
这不是错了吗? Spring 对 Java 配置的处理是否存在错误?
documentation 对此进行了解释
For a fallback match, the bean name is considered a default qualifier value. Thus you can define the bean with an id "main" instead of the nested qualifier element, leading to the same matching result. However, although you can use this convention to refer to specific beans by name,
@Autowired
is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even with the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id
所以,不,这不是错误,这是预期的行为。如果按类型自动装配找不到单个匹配的 bean,bean id(名称)将用作后备。