欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中基于xml配置管理Bean的步驟

 更新時間:2023年11月24日 10:35:28   作者:陳橘又青  
Spring容器通常理解為BeanFactory或者ApplicationContext,我們知道spring的IOC容器能夠幫我們創(chuàng)建對象,對象交給spring管理之后我們就不用手動去new對象,這篇文章主要介紹了Spring中基于xml配置管理Bean的步驟,需要的朋友可以參考下

前言

在之前的學習中我們知道,容器是一個空間的概念,一般理解為可盛放物體的地方。在Spring容器通常理解為BeanFactory或者ApplicationContext。我們知道spring的IOC容器能夠幫我們創(chuàng)建對象,對象交給spring管理之后我們就不用手動去new對象。

那么Spring是如何管理Bean的呢?

一、概念

簡而言之,Spring bean是Spring框架在運行時管理的對象。Spring bean是任何Spring應(yīng)用程序的基本構(gòu)建塊。你編寫的大多數(shù)應(yīng)用程序邏輯代碼都將放在Spring bean中。

Spring bean的管理包括:

  • 創(chuàng)建一個對象
  • 提供依賴項(例如其他bean,配置屬性)
  • 攔截對象方法調(diào)用以提供額外的框架功能
  • 銷毀一個對象

Spring bean是框架的基本概念。作為Spring的用戶,你應(yīng)該對這個核心抽象有深刻的理解。

二、創(chuàng)建Bean對象的三種方式

2.1、使用默認構(gòu)造函數(shù)創(chuàng)建方式

2.1.1、定義Bean

public class UserServiceImpl {
}

2.1.2、主配置文件中配置bean

<!-- 方式一:使用默認構(gòu)造函數(shù)方式創(chuàng)建Bean   -->
<beans>
	<bean id="userService" class="cn.bdqn.UserServiceImpl"></bean>
</beans>

2.1.3、測試Bean

@Test
public void testUserService() throws Exception{
        // 1、讀取主配置文件信息,獲取核心容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // 2、從容器中根據(jù)id獲取對象(bean)
        UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
        // 3、打印bean
        System.out.println(userService);
}

2.1.4、注意點

此種方式采用的就是通過默認構(gòu)造函數(shù)的方式創(chuàng)建Bean,假設(shè)我們給UserServiceImpl添加了一個帶參的構(gòu)造方法,則運行會報錯,原因在于當我們?yōu)槟硞€類自定義構(gòu)造方法的時候,Java編譯器便不會為該類提供默認的不帶參數(shù)的構(gòu)造方法了。

2.2、使用工廠中的實例方法創(chuàng)建方式

2.2.1、定義工廠

// UserService的工廠,作用是創(chuàng)建UserServiceBean對象
public class UserServiceImplFactory {
    public UserServiceImpl createUserService(){
        return new UserServiceImpl();
    }
}

2.2.2、定義Bean

public class UserServiceImpl {
}

2.2.3、主配置文件中配置Bean

<beans>
  	<!-- 方式二:使用工廠中提供的實例方法創(chuàng)建Bean   -->
	<!-- 第一步:把該工廠定義出來   -->
    <bean id="userServiceFactory" class="cn.bdqn.UserServiceImplFactory"/>
    <!-- 第二步:定義Bean(通過userServiceFactory中提供的實例方法)-->
    <bean id="userService" factory-bean="userServiceFactory" 
 						   factory-method="createUserService"/>
</beans>

2.2.4、測試

@Test
public void testUserService() throws Exception{
        // 1、讀取主配置文件信息,獲取核心容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // 2、從容器中根據(jù)id獲取對象(bean)
        UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
        // 3、打印bean
        System.out.println(userService);
}

2.3、使用工廠中的靜態(tài)方法創(chuàng)建方式

2.3.1、定義工廠

// UserService的工廠,作用是創(chuàng)建UserServiceBean對象
public class UserServiceImplFactory {
    public static UserServiceImpl createUserService(){
        return new UserServiceImpl();
    }
}

2.3.2、定義Bean

public class UserServiceImpl {
}

2.3.3、主配置文件中配置Bean

<beans>
    <!-- 方式三:使用工廠中提供的靜態(tài)方法創(chuàng)建Bean   -->
    <!-- 定義Bean(通過工廠類的靜態(tài)方法創(chuàng)建)   -->
    <bean id="userService" class="cn.bdqn.UserServiceImplFactory" 
          				   factory-method="createUserService">
    </bean>
</beans>

2.3.4、測試

@Test
public void testUserService() throws Exception{
        // 1、讀取主配置文件信息,獲取核心容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // 2、從容器中根據(jù)id獲取對象(bean)
        UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
        // 3、打印bean
        System.out.println(userService);
}

三、Bean對象的作用域

3.1、說明

? Spring對Bean的默認的作用域(作用范圍)是singleton【單例】

3.2、作用域類型

  • singleton:單例的(默認值),只會new一次。
  • prototype:多例的,用到一次就會new一次。
  • request:作用于web應(yīng)用的請求范圍,Spring創(chuàng)建這個類之后,將這個類存到request范圍內(nèi)。
  • session:應(yīng)用于web項目的會話范圍,Spring創(chuàng)建這個類之后,將這個類存到session范圍內(nèi)。
  • global-session:作用于集群環(huán)境的會話范圍(全局會話范圍),當不是集群環(huán)境時,它就是session。

3.3、注意細節(jié)

實際開發(fā)中用得最多的就是singleton和prototype,在整合struts2的時候使用prototype,在整合SpringMVC的時候使用singleton。

3.4、如何修改Bean的作用域

bean標簽的scope屬性,作用:指定bean的作用范圍。

3.5、測試

3.5.1、測試singleton單例

public class UserServiceImpl {
}
public class UserServiceImpl {
}
@Test
public void testUserService() throws Exception{
        // 1、讀取主配置文件信息,獲取核心容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // 2、從容器中根據(jù)id獲取對象(bean)
        UserServiceImpl userService1 = (UserServiceImpl) ac.getBean("userService");
        UserServiceImpl userService2 = (UserServiceImpl) ac.getBean("userService");
        // 3、打印bean
        System.out.println(userService1 == userService2); // true
}

3.5.2、測試prototype多例

@Test
public void testUserService() throws Exception{
        // 1、讀取主配置文件信息,獲取核心容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // 2、從容器中根據(jù)id獲取對象(bean)
        UserServiceImpl userService1 = (UserServiceImpl) ac.getBean("userService");
        UserServiceImpl userService2 = (UserServiceImpl) ac.getBean("userService");
        // 3、打印bean
        System.out.println(userService1 == userService2); // true
}
<bean id="userService" class="cn.bdqn.UserServiceImpl" scope="prototype"/>
 @Test
public void testUserService() throws Exception{
        // 1、讀取主配置文件信息,獲取核心容器對象
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        // 2、從容器中根據(jù)id獲取對象(bean)
        UserServiceImpl userService1 = (UserServiceImpl) ac.getBean("userService");
        UserServiceImpl userService2 = (UserServiceImpl) ac.getBean("userService");
        // 3、打印bean
        System.out.println(userService1 == userService2); // false
}

四、Bean對象的生命周期

4.1、單例對象

4.1.1、說明

出生:當容器創(chuàng)建時對象出生
活著:只要容器還在,對象一直活著
死亡:容器銷毀,對象消亡

4.1.2、測試

4.1.2.1、定義Bean
public class UserServiceImpl {
    public UserServiceImpl(){
        System.out.println("對象的構(gòu)造方法執(zhí)行了");
    }
    public void init(){
        System.out.println("對象初始化了");
    }
    public void destroy(){
        System.out.println("對象銷毀了");        
    }
}
4.1.2.2、主配置文件中配置Bean
<beans>
	<bean id="userService" class="cn.bdqn.UserServiceImpl"
          scope="singleton" init-method="init" destroy-method="destroy"/>
</beans>
4.1.2.3、測試
@Test
public void testUserService() throws Exception{
    // 1、讀取主配置文件信息,獲取核心容器對象
    ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    ac.close();
}
// 結(jié)果:對于單例對象來說,只要容器創(chuàng)建了,那么對象就創(chuàng)建了。類似于立即加載。
4.1.2.4、測試結(jié)果

對象的構(gòu)造方法執(zhí)行了
對象初始化了
對象銷毀了

總結(jié):單例對象的生命周期和容器相同

4.2、多例對象

4.2.1、說明

出生:當我們使用對象時spring框架為我們創(chuàng)建
活著:對象只要是在使用過程中就一直活著。
死亡:當對象長時間不用,且沒有別的對象引用時,由Java的垃圾回收器回收

4.2.2、測試

4.2.2.1、定義Bean
public class UserServiceImpl {
    public UserServiceImpl(){
        System.out.println("對象的構(gòu)造方法執(zhí)行了");
    }
    public void init(){
        System.out.println("對象初始化了");
    }
    public void destroy(){
        System.out.println("對象銷毀了");        
    }
}
4.2.2.2、主配置文件中配置Bean
<beans>
	<bean id="userService" class="cn.bdqn.UserServiceImpl"
          scope="prototype" init-method="init" destroy-method="destroy"/>
</beans>
4.2.2.3、測試1
@Test
public void testUserService() throws Exception{
   	// 1、讀取主配置文件信息,獲取核心容器對象
    ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    ac.close();
}
// 結(jié)果:什么都不輸出,說明容器啟動的時候,對于多例對象來說并不會創(chuàng)建
4.2.2.4、測試2
@Test
public void testUserService() throws Exception{
   	// 1、讀取主配置文件信息,獲取核心容器對象
    ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
    UserServiceImpl userService = (UserServiceImpl) ac.getBean("userService");
    System.out.println(userService);
    ac.close();
}
/**
	結(jié)果:
		對象的構(gòu)造方法執(zhí)行了
		對象初始化了
	說明:
		對于多例對象來說,只有等到真正使用到該對象的時候才會創(chuàng)建。類似于懶加載。
**/

? 對于多例的Bean,Spring框架是不負責管理的

五、總結(jié)

在這里插入圖片描述

到此這篇關(guān)于Spring中基于xml配置管理Bean的步驟的文章就介紹到這了,更多相關(guān)spring 管理Bean內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論