Spring4基礎 學習筆記(1) ---- Bean
阿新 • • 發佈:2018-03-27
mil 其他 使用 得到 cat contex life stp cno
Bean的裝配(創建)
默認裝配方式:getBean的時候調用無參構造器(沒有會出錯)
動態工廠bean:
<bean id="factory" class="Dynamic_Bean_Factory.ServiceFactory"/>
<!-- someSerice對象是由對應工廠對應方法創建 -->
<bean id="someService"factory-bean="factory"factory-method="getSomeSevice"/>
對象由工廠創建但是代碼裏沒有工廠,在配置文件裏配置
ISomeService service = (ISomeService) ac.getBean("someService");
靜態工廠bean:
工廠創建bean方法為static的
<!-- 由class對應工廠的對應靜態方法創建的對象 -->
<bean id="someService" class="Static_Bean_Factory.ServiceFactory" factory-method="getSomeService"/>
ISomeService service = (ISomeService) ac.getBean("someService" );
Bean的作用域:
1.prototype原型模式:每次getBean都會創建新的Bean,且在使用的時候才會創建
<bean id="someService" class="service.ISomeServiceImpl" scope="prototype"/>
2.singleton單例模式:每次getBean都是同一個對象,初始化applicationContext容器時候創建(默認值)
3.request:對於每次http請求都產生不同Bean
4.session:對於不同的session有不同的Bean
註:對於scope的值request、session、global session只有在Web應用中使用Spring時,才有效。
Bean後處理器:是一個特殊的Bean,容器中所有的Bean在初始化時,均會自動執行該類的兩個方法。
由於該Bean由其他Bean自動調用,所以該Bean沒有Id。
兩個方法:參數1初始化的bean,參數2beanName和bean的id屬性對應
//bean初始化完成之前
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
//bean初始化完成之後
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
Bean後處理器的應用:
1.修改after方法的返回值為bean的代理對象實現增強
2.為特定的bean增強:增加Condition:判斷beanName
bean的初始化前和銷毀前可以調用bean的方法:
init-method="" destroy-method=""/>
其中銷毀方法的執行有兩個要求:
1)被銷毀的對象需要是singleton
2)容器要顯式地關閉
對象默認是singleton的;appplicationContext接口沒有定義關閉方法,定義在實現類中,需要將applicationCnotext強轉:
((ClassPathXmlApplicationContext)ac).close();
Bean的生命周期(可控點):
1.bean的無參構造器
2.屬性的setter
<bean id="someService" class="lifeOfBean.ISomeServiceImpl" >
<property name="adao" value="AAA"/>
<property name="bdao" value="BBB"/>
</bean>
3.如果bean實現了beanNameAware接口:
@Override
public void setBeanName(String name) {
// TODO Auto-generated method stub
System.out.println("Step3 : beanName = " + name);
}
4.如果bean實現了BeanFactoryAware接口:
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
System.out.println("Step4 : 獲取到beanFactroy");
}
5.bean後處理器的before方法
6.如果bean實現了InitializingBean接口:
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("Step6 : InitializingBean接口的afterPropertiesSet方法");
}
標誌著bean的初始化工作完成
7.bean配置的init-method方法:
<bean id="someService" class="lifeOfBean.ISomeServiceImpl" init-method="init_method">
8.bean後處理器的after方法
9.bean的主業務方法
10.如果bean實現了DisposableBean接口(Disposable:一次性):
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("Step10 : ");
}
這個方法的執行需要對象是單例的和容器手工關閉
11.bean配置的destory-method
匿名bean:一個bean使用byType方式找查找另一個bean,另一個bean可以沒有id
內部bean:將bean放在property內,id為property的name :
<bean id="stu" class="di01.Student">
<property name="">
<bean class=""></bean>
</property>
</bean>
匿名bean:一個bean使用byType方式找查找另一個bean,另一個bean可以沒有id
內部bean:將bean放在property內,id為property的name :
<bean id="stu" class="di01.Student">
<property name="">
<bean class=""></bean>
</property>
</bean>
同類抽象bean:
bean可以繼承另一個bean:
<bean parent="">會繼承parent的class和property
用來被繼承的bean沒有意義,將abstract屬性定義為true
異類抽象bean:
被繼承的bean沒有class屬性,必須將abstract定義為true
如有錯誤,歡迎指正。
文章為博主看動力節點Spring教學視頻總結的筆記,作為以後查閱使用。
Spring主要作用為“解耦” 根據功能不同可以將系統中的代碼分為: 主業務邏輯 系統級業務邏輯:沒有具體的專業業務應用場景,為主業務提供系統級服務,例如日誌、安全、事務等 Spring根據代碼功能特點,將降低耦合度方式分為兩類: IoC與AOP IoC使得主業務在相互調用過程中不用再自己維護關系,即不用再自己創建要使用的對象了。而是有Spring容器統一管理,自動“註入”。 AOP使得系統級服務得到了最大復用,且不用再由程序員手工將系統級服務混雜到主業務邏輯中了,而是由Spring容器統一完成“織入”。 Spring作為一個容器,可以管理對象的生命周期、對象和對象之間的依賴關系。可以通過配置文件,來定義對象,以及設置與其他對象的依賴關系。 IoC(Inversion of Control) AOP(Aspect Orient Programming) IoC: 依賴查找:JNDI 依賴註入 配置文件:applicationContext.xml(名字隨意,官方建議) applicationContext容器和BeanFactory容器區別: 前者在初始化時創建對象,後者在使用時創建Spring4基礎 學習筆記(1) ---- Bean