歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring基礎學習筆記-Bean的基礎知識

Spring基礎學習筆記-Bean的基礎知識

日期:2017/3/1 9:14:27   编辑:Linux編程

一、 Bean的定義,初始化,使用和銷毀
二、ref指定依賴的三種模式
三、Bean的五種自動裝配模式(autowire)
四、Bean依賴檢查的4種模式:配合atuowire使用,dependency-check=""
五、集合的注入方式
六、管理Bean

config.xml文件
<!--Bean的配置文檔-->
<!--首先定義為XML的方式來存儲Bean的配置-->
<?xml version="1.0" encoding="UTF-8"?>
<!--聲明使用的是http://www.springframework.org/dtd/spring-beans.dtd-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframenwork.org/dtd/spring-beans.dtd">
<!--配置Bean的開始,根節點Beans中包含一個或多個Bean元素-->
<beans>
<bean id="" class="" init-mehod="" destory-method="" autowire="" dependecy-check="">
<property>
<value></value>
</property>
</bean>

</beans>

一、 Bean的定義,初始化,使用和銷毀

1.Bean的定義
2.初始化
init-method方法
實現InitializingBean接口,增加afterPropertiesSet()方法;
3.Bean的使用
使用有三種方式:
1)Beanwrapper
HelloWorld hellworld=new HelloWorld();
BeanWrapper bw=new BeanWrapperImpl(helloworld);
bw.setPropertyValue("msg","helloworld");
bw.getPropertyValue("msg");

2)BeanFactory
InputStream is=new FileInputStream("config.xml");
xmlBeanFactory factory=new xmlBeanFactory(is);
HelloWorld helloWorld=(HelloWorld)factory.getBean("HelloWorld");
helloWorld.getMsg();

3)ApplicationContext
ApplicationContext actx =new FileSystemXmlApplicationContext("config.xml");
HelloWorld HelloWorld = (HelloWorld)actx.getBean("HelloWorld");
HelloWorld.getMsg();


4.Bean的銷毀
1)destory-method
2)實現org.springframework.beans.factory.DisposableBean接口,增加destory()方法

二、ref指定依賴的三種模式
1.local
2.bean
3.parent
<property>
<ref="local"/>||<ref="bean"/>||<ref="parent"/>
<property>

三、Bean的五種自動裝配模式(autowire)
1.byName
2.byType
3.constructor
4.autodetect:有constructor就調用constructor,沒有的用byType
5.no:默認,不自動裝配

四、Bean依賴檢查的4種模式:配合atuowire使用,dependency-check=""
1.simple 對基本數據類型,字符串等進行檢查
2.object 對於依賴的對象進行檢查
3.all (包含simple和object)
4.none

五、集合的注入方式
1.list-Lis
<property name="">
<list>
<value></value>
<value></value>
<value></value>
</list>
</property>
2.set-Set
<property name="">
<set>
<value></value>
<value></value>
<value></value>
</set>
</property>
3.map-Map
<property name="">
<map>
<entry key="">
<value></value>
</entry>
</map>
</property>

4.props-Properties
<property name="">
<props>
<prop key="">HelloWorld</prop>
</props>
</propertiry>


六、管理Bean
1.BeanWrapper
2.BeanFactory
3.ApplicationContext

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved