第1章:spring注入/1.5 autowire:自动装载/2.1 概念
- 概念
- 指的是不用显式地指定绑定依赖项,而是由spring容易根据某些信息进行动态依赖绑定,具体的信息包括:参数类型、方法名称或者指定名称
第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.1 XML配置方式
- 参数类型:基础参照: 第1章:spring注入/1.1 元数据配置/1.2 XML配置方式/1.2.4 XML配置方式实例源代码(Maven工程,已编译通过)
- 概念:指的是某个bean的待注入的属性(用setter方法标识)通过其它相同的数据类型的bean进行注入,这里的相同指的是接口的实现类或者超类继承的子类或者类本身
- 格式:
<bean id="bean的名称" class="含包名的类名" autowire="byType">
- 说明:
- bean的名称、含包名的类名参照: 第1章:spring注入/1.1 元数据配置/1.2 XML配置方式/1.2.2 XML文件结构/1.2.2.3 Bean的定义
- autowire="byName":固定写法
- 举例:
<!-- AccountServiceImpl类中有个setAccountDao属性,它是通过类型AccountDao进行注入的 -->
<bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl" autowire="byType"/>
<!-- accountDao这个bean的类型AccountDaoInMemoryImpl刚好是AccountDao这个接口的实现类 -->
<bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl"/>
- 注意:如果有多个bean满足某个待注入属性则注入会失败
- 举例:
<bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl" autowire="byType"/>
<bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl"/>
<bean id="accountDaoJdbc" class="com.wiley.beginningspring.ch2.AccountDaoJdbcImpl"/>
- AccountDaoInMemoryImpl、AccountDaoJdbcImpl都是accountService的实现类
- 报错:
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountService' defined in class path resource [com/wiley/beginningspring/ch2/ch2-beans.xml]: Unsatisfied dependency expressed through bean property 'accountDao': : No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDao,accountDaoJdbc; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDao,accountDaoJdbc
- 举例:
第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.2 Java配置方式
- Java配置方式:基础参照: 第1章:spring注入/1.1 元数据配置/1.3 JAVA配置方式/1.3.4 Java配置方式实例源代码(Maven工程,已编译通过)
- 概念:指的是某个bean的待注入的属性(用setter方法标识)通过其它相同的数据类型的bean进行注入,这里的相同指的是接口的实现类或者超类继承的子类或者类本身
- 格式:
@Bean(autowire=Autowire.BY_TYPE)
- 说明:
- Autowire是枚举类型,有三个值
- BY_TYPE:通过类型
- BY_NAME:通过名称
- NO:不自动装载
- Autowire是枚举类型,有三个值
- 举例:
@Bean(autowire=Autowire.BY_TYPE)
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;
}
//通过类型匹配的话,这个accountDao()方法和accountDaoJdbc()要二选一
// @Bean
// public AccountDao accountDaoJdbc() {
// AccountDaoJdbcImpl bean = new AccountDaoJdbcImpl();
// return bean;
// }
- 注意:如果有多个bean满足某个待注入属性则注入会失败
- 举例:
@Bean(autowire=Autowire.BY_TYPE)
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;
}
- accountDao、accountDaoJdbc 这两个类型都匹配 AccountServiceImpl.setAccountDao,所以注入失败
- 报错:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDao,accountDaoJdbc
- 举例:
第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 参数类型/2.2.1.3 注解方式
- 注解方式:
- @Autowired的默认方式就是by_Type,所以这种方式完成可以参照:第1章:spring注入/1.1 元数据配置/1.4 注解方式/1.4.4 注解方式实例源代码(Maven工程,已编译通过)
- 注意:如果有多个bean满足某个待注入属性则注入会失败
- 举例:
-
@Autowired
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
-
@Repository
public class AccountDaoInMemoryImpl implements AccountDao {
-
@Repository
public class AccountDaoJdbcImpl implements AccountDao {
- 类AccountDaoInMemoryImpl、AccountDaoJdbcImpl都是AccountDao接口的实现类,而@Autowired默认就是以类型为注入标准的,所以会有两个注入满足条件,最后会报错
-
- 报错:
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wiley.beginningspring.ch2.AccountDao] is defined: expected single matching bean but found 2: accountDaoInMemoryImpl,accountDaoJdbcImpl
- 举例:
第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 方法名称/2.2.2.1 XML配置方式
- XML配置方式
- 参照基础: 第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.1 XML配置方式
- 不同:
- autowire="byType" 修改为 autowire="byName"
- 名称对比指的是:setter方法的名称和bean的名称对比
第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 方法名称/2.2.2.2 Java配置方式
- Java配置方式
- 参照基础: 第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.1 参数类型/2.2.1.2 Java配置方式
- 不同:
- autowire=Autowire.BY_TYPE 修改为 autowire=Autowire.BY_Name
- 名称对比指的是:setter方法的名称和bean的名称(用@Bean标注的方法名)对比
第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 方法名称/2.2.2.3 注解方式
- 注解方式
- 参照基础: 第1章:spring注入/1.5 autowire:自动装载/2.2 装载类型/2.2.2 参数类型/2.2.1.3 注解方式
- 不同:
- 在@Autowired下面再增加注解:@Qualifier("指定的名称"),比如:@Qualifier("accountDao")
@Autowired
@Qualifier("accountDao")
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
- 在@Repository下面再增加注解:@Qualifier("指定的名称"),比如:@Qualifier("accountDao")
@Repository
@Qualifier("accountDao")
public class AccountDaoInMemoryImpl implements AccountDao {
- 名称对比指的是:@Qualifier指定的名称对比
- 在@Autowired下面再增加注解:@Qualifier("指定的名称"),比如:@Qualifier("accountDao")