一.Bean的自動裝配
1.1三種裝配機制
(1).xml中顯示配置
(2).自動裝配
(3)java中顯示配置
1.2自動裝配的兩個操作
組件掃描,自動裝配
DEMO
public class Cat {
public void shout() {
System.out.println("miao~");
}
}
public class Dog {
public void shout() {
System.out.println("wang~");
}
}
public class User {
private Cat cat;
private Dog dog;
private String str;
}
1.3基於配置文件的自動裝配
ByName(按名稱自動裝配)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User" autowire="byName">
<property name="str" value="qinjiang"/>
</bean>
</beans>
如果把cat的bean id改成cat111,就會報空指針,小結如下:
1.將查找其類中所有的set方法名,例如setCat,獲得將set去掉並且首字母小寫的字符串,即cat。
2.去spring容器中尋找是否有此字符串名稱id的對象。
3.如果有,就取出注入;如果沒有,就報空指針異常。
byType(按類型自動裝配)小結如下:
1.將user的bean配置修改一下 : autowire=“byType”
2.如果spring容器中類型不唯一,那麼就會報不唯一的異常
1.4.基於註解的自動裝配
1.準備工作,配置文件引入context文件頭
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
2.開啓註解支持
<context:annotation-config/>
3.代碼測試
public class User {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String str;
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getStr() {
return str;
}
}
<context:annotation-config/>
<bean id="dog" class="com.kuang.pojo.Dog"/>
<bean id="cat" class="com.kuang.pojo.Cat"/>
<bean id="user" class="com.kuang.pojo.User"/>
二.基於註解開發
2.1 要引入context約束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2.2 Bean的實現
1.配置掃描的包
<!--指定註解掃描包-->
<context:component-scan base-package="com.kuang.pojo"/>
2.在指定包下增加註解和屬性注入
@Component("user")
// 相當於配置文件中 <bean id="user" class="當前註解的類"/>
public class User {
@Value("秦疆")
// 相當於配置文件中 <property name="name" value="秦疆"/>
public String name;
}
基於註解的ioc配置和xml配置要實現的功能都是一樣的,都是要降低程序間的耦合,只是配置的形式不一樣。
使用@Componet註解來配置管理的資源(即產生bean)
賬戶的業務層實現類
@Component("accountService")
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
}
賬戶的持久層實現類
@Component("accountDao")
public class AccountDaoImpl implements IAccountDao {
private DBAssit dbAssit; }
當我們基於註解整合時,導入約束的時候需要多導入一個context名稱空間下的約束
告知 spring 創建容器時要掃描的包
<context:component-scan base-package="com.itheima"></context:component-scan
>
@Component屬性有:value:指定bean的id,如果不指定value屬性,默認是bean的id當前類的類名,首字母小寫,然後和@component等價的註解有@Controller,@Service,@Repository,然後他們分別用在表現層,業務層,持久層
注入數據
相當於,
@Aurowired
作用: 自動按照類型注入。當使用註解注入屬性時,set方法可以省略。它只能注入其他 bean 類型。當有多個 類型匹配時,使用要注入的對象變量名稱作爲 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到 就報錯。
@Qualifier
作用: 在自動按照類型注入的基礎之上,再按照 Bean 的 id 注入。它在給字段注入時不能獨立使用,必須和 @Autowired一起使用;但是給方法參數注入時,可以獨立使用。
屬性: value:指定 bean 的 id。
@Resource
作用: 直接按照 Bean 的 id 注入。它也只能注入其他 bean 類型。
屬性: name:指定 bean 的 id。
@Value
作用: 注入基本數據類型和 String 類型數據的
屬性: value:用於指定值
改變作用範圍
相當於:<bean id="" class="" scope="">
@Scope
作用: 指定 bean 的作用範圍。
屬性: value:指定範圍的值。
取值:singleton prototype request session globalsession
和生命週期相關
相當於:<bean id="" class="" init-method="" destroy-method="" />
@PostConstruct
作用: 用於指定初始化方法。
@PreDestroy
作用: 用於指定銷燬方法。
參考基於註解開發