1、spring容器中Bean對象的使用范圍控制
a、控制對象創建方式(使用范圍),在<bean>元素中使用scope屬性控制,scope可以支持singleton或prototype,默認值是singleton
<bean scope= "singleton"> 該組件在spring容器里只有一個bean對象。每次取出的bean都是同一個bean,相當於單例模式
<bean scope = "prototype">該組件每次使用getBean("")都會返回一個新的對象
例如我們自定義了一個ExampleBean類:
public class ExampleBean { public void execute() { System.out.println("調用了execute方法"); } }
在applicationContext.xml中進行配置,默認使用scope="singleton"
<!-- 創建一個ExampleBean對象 --> <bean id ="example" class="com.zlc.test.ExampleBean"></bean>
我們測試一下每次取出的bean是否為同一個bean:
public static void main(String[] args) { String conf = "applicationContext.xml"; //創建容器對象 ApplicationContext ac = new ClassPathXmlApplicationContext(conf); //得到Example對象 ExampleBean e1 = ac.getBean("example",ExampleBean.class); ExampleBean e2 = ac.getBean("example",ExampleBean.class); System.out.println(e1 == e2); }
運行結果:
由此可見:每次我們從容器中取出的對象都是同一個對象
接下來我們將scope改成"prototype",例如:
<!-- 創建一個ExampleBean對象 --> <bean id ="example" class="com.zlc.test.ExampleBean" scope="prototype"></bean>
重新運行上面的代碼,看看兩次取出的對象是否一致,運行結果如下:
由此可知,當scope設置為"prototype"時,每次從Spring容器中獲取該對象時,容器都會返回一個新的對象
2、spring容器中對象(組件)的初始化控制
在某些時候,我們需要在創建完對象后,對其進行一些初始化操作,例如解析某個xml文件,讀取某些相關的配置信息等,使用spring容器管理和創建該對象(組件時),我們就可以指定某些初始化控制。
(1)在構造器中進行某些初始化操作,例如:
public class ExampleBean { public ExampleBean() { System.out.println("正在進行初始化操作。。。。"); } public void execute() { System.out.println("調用了execute方法"); } }
public static void main(String[] args) { String conf = "applicationContext.xml"; //創建容器對象 ApplicationContext ac = new ClassPathXmlApplicationContext(conf); //得到Example對象 ExampleBean e1 = ac.getBean("example",ExampleBean.class); ExampleBean e2 = ac.getBean("example",ExampleBean.class); System.out.println(e1 == e2); }
運行代碼,我們可以看到在spring使用構造器創建該對象(組件)時,調用了構造器中的初始化方法:
(2)指定某個方法作為初始化方法,例如我們在ExampleBean中新增一個init方法進行初始化操作
public class ExampleBean { public ExampleBean() { System.out.println("正在進行初始化操作。。。。"); } public void execute() { System.out.println("調用了execute方法"); } public void init() { System.out.println("正在執行初始化方法中的操作。。。。"); } }
在xml文件中我們指定其初始化方法。
<!-- 創建一個ExampleBean對象 --> <bean id ="example" class="com.zlc.test.ExampleBean" scope="prototype" init-method = "init"></bean>
運行以上代碼,結果如下:
由此可以看出:spring容器創建完對象后,將會執行xml文件中指定的初始化方法,單例模式中初始化方法只會執行一次
3、指定對象(組件)的銷毀方法
在xml文件中,我們為其指定一個屬性destroy-method,它指定對象的銷毀方法。
<!-- 創建一個ExampleBean對象 --> <bean id ="example" class="com.zlc.test.ExampleBean" init-method = "init" destroy-method="destroy"> </bean>
我們試着關閉spring容器對象以觸發該單例對象的destroy方法:
public static void main(String[] args) { String conf = "applicationContext.xml"; //創建容器對象 AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf); //得到Example對象 ExampleBean e1 = ac.getBean("example",ExampleBean.class); ExampleBean e2= ac.getBean("example",ExampleBean.class); ac.close();//釋放spring容器對象,它會自動調用單例的destroy方法。 }
運行結果如下:
由此我們可以看出,spring容器在銷毀單例對象前,先會去調用該對象指定的destroy方法
注意:只有在銷毀單例對象時,才會調用該對象的destroy方法,若其scope設置為:prototype,則不會調用該方法
總結:指定對象的銷毀方法,使用<bean destroy-method=""></bean>,在滿足以下條件時觸發其產生作用:
1、組件對象為單例模式
2、調用AbstractApplicationContext對象的close()方法