-
xml方式的Bean自動注入(<beans>標(biāo)簽的屬性default-autowire=byName)
No:不做任何操作,默認(rèn)選項(xiàng)。
byname:根據(jù)setXXX()方法的set后的名稱進(jìn)行自動注入(首字母不區(qū)分大小寫)。查找xml中有沒有<bean>標(biāo)簽id的名稱與屬性完全一致的,如果有則自動注入,并且執(zhí)行set方法,基于set方法,如果沒有不報錯,該屬性置為NULL,并且不執(zhí)行set()。
byType:根據(jù)類型自動注入,如果容器中存在一個與指定屬性類型相同的bean,那么該屬性自動注入;如果存在多個該類型bean,那么拋出異常,并指出不能使用byType方式進(jìn)行自動裝配,如果沒有找到相匹配的bean,則什么事都不發(fā)生,該屬性置為null基于set方法。
Constructor:與byType方式類似,不同之處在于它應(yīng)用于構(gòu)造器參數(shù)。如果容器中沒有找到與構(gòu)造器參數(shù)類型一個bean,那么拋出異常。
Spring官方文檔對自動注入屬性的四種類型解釋
案例:(原理:IOC容器加載XML配置文件,由于設(shè)置了自動按名稱進(jìn)行注入,所以當(dāng)獲取Bean1的實(shí)例時,查找XML文件中有沒有Id為set方法后的名稱(去掉set,并且不區(qū)分大小寫),如果有則進(jìn)行自動注入)
步驟1:
編寫兩個類,并在其中一個類中引用其他類作為自己的屬性,而且必須提供set方法。
步驟2:
編寫xml文件
<beans>標(biāo)簽添加?default-autowire="byName"
并且對著兩個類進(jìn)行<bean>配置。
代碼:
public class Bean1{
private Bean2 bean2;
public Bean2 getBean3() {
return bean2;
}
public void setBean3(Bean2 bean2) {
this.bean2 = bean2;
}
}
xml:<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"?
? ? ? ? default-autowire="byName">
? ?<bean id="bean1" class="main.java.com.imooc.Bean1"></bean>
? ?<bean id="bean3" class="main.java.com.imooc.Bean2"></bean>
?</beans>
結(jié)果:
Bean1 [bean2=main.java.com.imooc.Bean2@2ec5f45d]
當(dāng)default-autowire="byType"時:則會根據(jù)屬性的類型查找xml中是否有和它相同的類型的<bean>(也就是class),如果有以一個則進(jìn)行注入,如果一個以上則報異常,沒有則為null。
通過構(gòu)造方法實(shí)現(xiàn)自動注入:
步驟1:提供構(gòu)造方法為屬性賦值。
代碼:
public class Bean1{
private Bean2 bean2;
public Bean1(Bean2 bean2) {
super();
this.bean2 = bean2;
}
步驟2:xml中設(shè)置default-autowired="constructor”,并且提供和有參構(gòu)造函數(shù)的參數(shù)相同類型的一個<bean>標(biāo)簽。
代碼:
<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"?
? ? ? ? default-autowire="constructor">
? ?<bean id="bean1" class="main.java.com.imooc.Bean1"></bean>
? ?<bean class="main.java.com.imooc.Bean2"></bean>
?</beans>
結(jié)果:
Bean2@5c5766b1
查看全部 -
Aware接口
定義:Spring中提供了一些以Aware結(jié)尾的接口,實(shí)現(xiàn)了Aware接口的Bean在被初始化后,可以獲取相應(yīng)資源,通過Aware接口,可以對Spring相應(yīng)資源進(jìn)行操作(慎重),前提配置<bean>標(biāo)簽,并使用ioc容器去記性加載。
ApplicationContextAware:Bean類實(shí)現(xiàn)該接口,通過該接口提供的方法,可以直接獲取spring上下文,而不用我們自己手動創(chuàng)建。
BeanNameAware:Bean類實(shí)現(xiàn)該接口,通過該接口提供的方法,可以直接獲取<bean>標(biāo)簽的Id。
ApplicationEventPublisherAware:提供事件的發(fā)布。
BeanClassLoaderAware:找到相關(guān)的類加載器。
案例:通過傳入的Bean的ID和傳入的ApplicationContext對象,來獲取Bean的對象(原理:IOC容器加載配置文件,通過<bean>標(biāo)簽,去實(shí)例化Bean,由于實(shí)現(xiàn)了XXXAware接口,通過setXXX方法,去獲取相應(yīng)的資源)
public class MoocApplicationContext implements ApplicationContextAware,BeanNameAware {
private String name;(通過這種方式就可以在其他方法中進(jìn)行使用該對象,例如判斷某個對象是否存在)
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
System.out.println("setApplicationContext的ApplicationContext:"+ac.hashCode());
System.out.println(ac.getBean(this.name,MoocApplicationContext.class));
}
@Override
public void setBeanName(String name) {
this.name=name;
System.out.println("setBeanNamename的方法"+name);
}
}
xml:
? ?<bean id="moocApplicationContext" class="MoocApplicationContext" ></bean>
測試:
@Test
public void testMooc(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
System.out.println("test的ApplicationContext:"+ac.hashCode());
}
結(jié)果:
setBeanNamename的方法moocApplicationContext
setApplicationContext的ApplicationContext:1516607441
MoocApplicationContext@4c7335b1
test的ApplicationContext:1516607441
查看全部 -
接口是用于隱藏具體實(shí)現(xiàn)和實(shí)現(xiàn)多態(tài)性的組件
IOC控制反轉(zhuǎn),就是控制權(quán)的轉(zhuǎn)移,應(yīng)用程序本身不負(fù)責(zé)依賴對象的創(chuàng)建和維護(hù),而是由外部容器(spring)負(fù)責(zé)創(chuàng)建和維護(hù)
查看全部 -
Bean容器初始化
查看全部 -
單元測試截圖
查看全部 -
IOC房屋中介
查看全部 -
控制反轉(zhuǎn),依賴注入
查看全部 -
spring中所有的對象都是bean
查看全部 -
@Autowired由Spring BeanPostProcessor處理,所以不能在自己的BeanPostProcessor或BeanFactoryPostProcessor中應(yīng)用@Autowired注解,否則會產(chǎn)生循環(huán)依賴
查看全部 -
<context:annotation-config>只會識別類中關(guān)于方法的注解
查看全部 -
AOP in Spring
查看全部 -
relevant concepts in AOP
查看全部 -
AOP implement types
查看全部 -
The definition of IoC
查看全部 -
如何定義好的切入點(diǎn)
查看全部
舉報