- 默認(rèn)裝配方式 通過getbean()方式從容器中獲取指定的Bean實例突颊,容器首先會調(diào)用Bean類的無參構(gòu)造器竿报,創(chuàng)建空值的實例對象句携。
@Test
public void test01() {
// 加載Spring配置文件疗锐,創(chuàng)建Spring容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba01/applicationContext.xml");
// 從容器中獲取指定的bean對象
ISomeService service = (ISomeService) ac.getBean("someService");
service.doFirst();
service.doSecond();
}
public SomeServiceImpl() {
System.out.println("===========");
}
若加入有參就會報錯比如SomeServiceImpl(int a);
- 動態(tài)工廠Bean(創(chuàng)建工廠對象衡载,調(diào)用方法)
public class ServiceFactory {
public ISomeService getSomeService() {
return new SomeServiceImpl();
}
}
@Test
public void test01() {
// 加載Spring配置文件搔耕,創(chuàng)建Spring容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba02/applicationContext.xml");
// 從容器中獲取指定的bean對象
// ServiceFactory sf = (ServiceFactory) ac.getBean("serviceFactory");
// ISomeService service = sf.getSomeService();
//降低類和類之間的耦合度,看不出實現(xiàn)類是誰,工廠是誰
ISomeService service = (ISomeService) ac.getBean("someService");
service.doFirst();
service.doSecond();
}
<bean id="serviceFactory" class="com.ba02.ServiceFactory"></bean>
<bean id="someService" factory-bean="serviceFactory" factory-method="getSomeService"></bean>
someService對象由serviceFactory工廠的getSomeService方法創(chuàng)建
- 靜態(tài)工廠Bean(直接調(diào)用靜態(tài)方法)
public class ServiceFactory {
public static ISomeService getSomeService() {
return new SomeServiceImpl();
}
}
<bean id="someService" class="com.ba03.ServiceFactory" factory-method="getSomeService"></bean>
someService對象由com.ba03.ServiceFactory類的getSomeService方法創(chuàng)建
4.Bean的作用域
<bean id="someService" class="com.ba04.SomeServiceImpl" scope="prototype"/>
scope="prototype"的bean容器在接受到該類型的對象的請求的時候弃榨,會每次都重新生成一個新的對象給請求方;(即真正使用時才創(chuàng)建菩收,每獲取一次,都會創(chuàng)建不同的對象)
scope="singleton"的bean是由容器來保證這種類型的bean在同一個容器內(nèi)只存在一個共享實例(容器初始化時就創(chuàng)建鲸睛,每次獲取的都是同一個對象娜饵。默認(rèn)的)
?官辈?箱舞??拳亿?5.Bean后處理器(這個還有一些問題)
public class MyBeanPostProcessor implements BeanPostProcessor {
// bean:當(dāng)前調(diào)用Bean后處理器的Bean對象
//beanname:Bean對象的id
@Override
public Object postProcessAfterInitialization(Object bean, String beanname) throws BeansException {
System.out.println("執(zhí)行****After****()");
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanname) throws BeansException {
System.out.println("執(zhí)行****Before****()");
return bean;
}
}
public void test01() {
// 加載Spring配置文件晴股,創(chuàng)建Spring容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba05/applicationContext.xml");
// 從容器中獲取指定的bean對象
ISomeService service1 = (ISomeService) ac.getBean("someService1");
service1.doFirst();
service1.doSecond();
System.out.println("----------------");
ISomeService service2 = (ISomeService) ac.getBean("someService2");
service2.doFirst();
service2.doSecond();
}
<bean id="someService1" class="com.ba05.SomeServiceImpl" />
<bean id="someService2" class="com.ba05.SomeServiceImpl" />
<bean class="com.ba05.MyBeanPostProcessor"></bean>
容器初始化之后對象初始化之前
6.定制Bean的生命始末
public void test01() {
// 加載Spring配置文件,創(chuàng)建Spring容器對象
ApplicationContext ac = new ClassPathXmlApplicationContext("com/ba06/applicationContext.xml");
// 從容器中獲取指定的bean對象
ISomeService service = (ISomeService) ac.getBean("someService");
service.doFirst();
service.doSecond();
// 銷毀方法的執(zhí)行有兩個要求:
// 1)被銷毀的對象需要是singleton的肺魁,即單例
// 2)容器要顯示關(guān)閉
((ClassPathXmlApplicationContext) ac).close();// 接口沒有實現(xiàn)類有
}
public void initAfter() {
System.out.println("初始化之后");
}
public void preDestory() {
System.out.println("銷毀之前");
}
<bean id="someService" class="com.ba06.SomeServiceImpl"
init-method="initAfter" destroy-method="preDestory" />
7.Bean的生命周期的可控點(diǎn)很多
step1.對象的創(chuàng)建
step2.執(zhí)行setAdao()
step3.beanName=someService
step4.獲取到beanFactory容器
step5.執(zhí)行****Before()
step6.當(dāng)前Bean初始化工作已經(jīng)完畢
step7.初始化之后
step8.執(zhí)行****After()
step9.執(zhí)行主業(yè)務(wù)方法
step10. 準(zhǔn)備銷毀工作电湘,進(jìn)行銷毀流程
step11.銷毀之前
8.id與name屬性的區(qū)別
id的命名需要滿足XML對ID屬性命名規(guī)范;必須以字幕開頭万搔,可以包含字母胡桨,數(shù)字,下劃線瞬雹,連字符昧谊,句號,冒號酗捌。
name屬性值則可以包含各種字符