日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 框架基础(02):Bean的生命周期,作用域,装配总结

發(fā)布時間:2025/3/16 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 框架基础(02):Bean的生命周期,作用域,装配总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文源碼:GitHub·點這里 || GitEE·點這里

一、裝配方式

Bean的概念:Spring框架管理的應用程序中,由Spring容器負責創(chuàng)建,裝配,設置屬性,進而管理整個生命周期的對象,稱為Bean對象。

1、XML格式裝配

Spring最傳統(tǒng)的Bean的管理方式。

  • 配置方式
<bean id="userInfo" class="com.spring.mvc.entity.UserInfo"><property name="name" value="cicada" /> </bean>
  • 測試代碼
ApplicationContext context01 = new ClassPathXmlApplicationContext("/bean-scan-02.xml"); UserInfo userInfo = (UserInfo)context01.getBean("userInfo") ; System.out.println(userInfo.getName());

2、注解掃描

在實際開發(fā)中:通常使用注解 取代 xml配置文件。

  • 常見注解
@Component <==> <bean class="Class"> @Component("id") <==> <bean id="id" class="Class"> @Repository :Mvc架構中Dao層Bean的注解 @Service:Mvc架構中Service層Bean的注解 @Controller:Mvc架構中Controller層Bean的注解
  • 使用案例
// 1、注解代碼塊 @Component("infoService") public class InfoServiceImpl implements InfoService {@Overridepublic void printName(String name) {System.out.println("Name:"+name);} }// 2、配置代碼塊 @ComponentScan // 組件掃描注解 public class BeanConfig {}
  • 測試代碼
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = BeanConfig.class) public class Test01 {@Autowiredprivate InfoService infoService ;@Testpublic void test1 (){infoService.printName("cicada");System.out.println(infoService==infoService);} }

3、XML配置掃描

上面使用 ComponentScan 注解,也可在配置文件進行統(tǒng)一的配置,效果相同,還簡化代碼。

<context:component-scan base-package="com.spring.mvc" />

4、Java代碼裝配

這種基于Configuration注解,管理Bean的創(chuàng)建,在SpringBoot和SpringCloud的框架中,十分常見。

  • 配置類代碼
@Configuration // 配置類注解 public class UserConfig {@Beanpublic UserInfo userInfo (){System.out.println("userInfo...");return new UserInfo() ;} }
  • 測試代碼
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = UserConfig.class) public class Test03 {@Autowiredprivate UserInfo userInfo ;@Autowiredprivate UserInfo userInfo1 ;@Testpublic void test1 (){/** userInfo...* true*/System.out.println(userInfo==userInfo1);} }

二、屬性值設置

上面是Bean的裝配幾種常見方式,下面來看看Bean屬性值設置,這里就基于Xml配置的方式。

1、基礎類型和集合

  • 配置代碼
<!-- 配置Employee公共屬性 --> <bean id="emp1" class="com.spring.mvc.entity.Employee"><property name="name" value="cicada" /><property name="id" value="1" /> </bean> <bean id="emp2" class="com.spring.mvc.entity.Employee"><property name="name" value="smile" /><property name="id" value="2" /> </bean> <!-- 配置Department屬性 --> <bean id="department" class="com.spring.mvc.entity.Department"><!-- 普通屬性值的注入 --><property name="name" value="IT部門" /><!-- 給數(shù)組注入值 --><property name="empName"><list><value>empName1</value><value>empName2</value><value>empName3</value></list></property><!-- 給List注入值:可以存放相同的值 --><property name="empList"><list><ref bean="emp1"/><ref bean="emp2"/><ref bean="emp1"/></list></property><!-- 配置Set屬性,相同的對象會被覆蓋 --><property name="empSet"><set><ref bean="emp1"/><ref bean="emp2"/><ref bean="emp1"/></set></property><!-- 配置Map屬性,key相同的話,后面的值會覆蓋前面的 --><property name="empMap"><map><entry key="1" value-ref="emp1" /><entry key="2" value-ref="emp2" /><entry key="2" value-ref="emp1" /></map></property><!-- 配置屬性集合 --><property name="pp"><props><prop key="pp1">Hello</prop><prop key="pp2">World</prop></props></property> </bean>
  • 測試代碼
public class Test05 {@Testpublic void test01 (){ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-03.xml");Department department = (Department) context.getBean("department");System.out.println(department.getName());System.out.println("--------------------->String數(shù)組");for (String str : department.getEmpName()){System.out.println(str);}System.out.println("--------------------->List集合");for (Employee smp : department.getEmpList()){System.out.println(smp.getId()+":"+smp.getName());}System.out.println("--------------------->Set集合");for (Employee emp : department.getEmpSet()){System.out.println(emp.getId()+":"+emp.getName());}System.out.println("--------------------->Map集合");for (Map.Entry<String, Employee> entry : department.getEmpMap().entrySet()){System.out.println(entry.getKey()+":"+entry.getValue().getName());}System.out.println("--------------------->Properties");Properties pp = department.getPp();System.out.println(pp.get("pp1"));System.out.println(pp.get("pp2"));} }

2、配置構造函數(shù)

根據(jù)配置的參數(shù)個數(shù)和類型,去映射并加載Bean的構造方法。

  • 配置代碼
<!-- 這里配置2個參數(shù),所有調(diào)用2個參數(shù)的構造函數(shù) --> <bean id="employee" class="com.spring.mvc.entity.Employee"><constructor-arg index="0" type="java.lang.String" value="cicada"/><constructor-arg index="1" type="int" value="1"/> </bean>
  • 測試代碼
public class Test06 {@Testpublic void test01 (){ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-04.xml");Employee employee = (Employee) context.getBean("employee");System.out.println(employee.getId()+":"+employee.getName());} }

3、配置繼承關系

  • 配置代碼
<!-- 配置父類信息 --> <bean id="student" class="com.spring.mvc.entity.Student"><property name="name" value="Spring" /><property name="age" value="22" /> </bean> <!-- 配置子類信息 --> <bean id="grade" class="com.spring.mvc.entity.Grade"><!-- 覆蓋 --><property name="name" value="Summer" /><property name="degree" value="大學" /> </bean>
  • 測試代碼
public class Test07 {@Testpublic void test01 (){ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-05.xml");Grade grade = (Grade) context.getBean("grade");/* Summer;0;大學 */System.out.println(grade.getName()+";"+grade.getAge()+";"+grade.getDegree());} }

三、作用域

作用域:用于確定spring創(chuàng)建bean實例個數(shù),比如單例Bean,原型Bean,等等。

類型說明
singletonIOC容器僅創(chuàng)建一個Bean實例,IOC容器每次返回的是同一個單例Bean實例,默認配置。
prototypeIOC容器可以創(chuàng)建多個Bean實例,每次返回的Bean都是新的實例。
request每次HTTP請求都會創(chuàng)建一個新的Bean,適用于WebApplicationContext環(huán)境。
session同一個HTTP Session共享一個Bean實例。不同HTTP Session使用不同的實例。
global-session同session作用域不同的是,所有的Session共享一個Bean實例。

四、生命周期

在Spring框架中Bean的生命周期非常復雜,過程大致如下:實例化,屬性加載,初始化前后管理,銷毀等。下面基于一個案例配置,會更加的清楚。

1、編寫B(tài)eanLife類

public class BeanLife implements BeanNameAware {private String name ;public String getName() {return name;}public void setName(String name) {System.out.println("設置名稱:"+name);this.name = name;}@Overridepublic void setBeanName(String value) {System.out.println("BeanNameAware..SetName:"+value);}public void initBean() {System.out.println("初始化Bean..");}public void destroyBean() {System.out.println("銷毀Bean..");}public void useBean() {System.out.println("使用Bean..");}@Overridepublic String toString() {return "BeanLife [name = " + name + "]";} }

2、定制加載過程

實現(xiàn)BeanPostProcessor接口。

public class BeanLifePostProcessor implements BeanPostProcessor {// 初始化之前對bean進行增強處理@Overridepublic Object postProcessBeforeInitialization(Object obj, String beanName) throws BeansException {System.out.println("初始化之前..."+beanName);return obj ;}// 初始化之后對bean進行增強處理@Overridepublic Object postProcessAfterInitialization(Object obj, String beanName) throws BeansException {System.out.println("初始化之后..."+beanName);// 改寫B(tài)ean的名稱if (obj instanceof BeanLife){BeanLife beanLife = (BeanLife)obj ;beanLife.setBeanName("myBeanLifeTwo");return beanLife ;}return obj ;} }

3、配置文件

<!-- 加載Bean的處理器 --> <bean class="com.spring.mvc.BeanLifePostProcessor" /> <!-- 指定初始化和銷毀方法 --> <bean id="beanLife" class="com.spring.mvc.entity.BeanLife"init-method="initBean" destroy-method="destroyBean"><property name="name" value="myBeanLifeOne" /> </bean>

4、測試過程

  • 測試代碼
public class Test08 {@Testpublic void test01 (){ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-06.xml");BeanLife beanLife = (BeanLife) context.getBean("beanLife");System.out.println("測試結果BeanLife:"+beanLife.getName()) ;beanLife.useBean();// 關閉容器((AbstractApplicationContext) context).close();} }
  • 輸出結果
1、設置名稱:myBeanLifeOne 2、BeanNameAware..SetName:beanLife 3、初始化之前...beanLife 4、初始化Bean.. 5、初始化之后...beanLife 6、BeanNameAware..SetName:myBeanLifeTwo 7、測試結果BeanLife:myBeanLifeOne 8、使用Bean.. 9、銷毀Bean..

這里梳理Bean的生命周期,過程十分清晰。

五、源代碼地址

GitHub·地址 https://github.com/cicadasmile/spring-mvc-parent GitEE·地址 https://gitee.com/cicadasmile/spring-mvc-parent

總結

以上是生活随笔為你收集整理的Spring 框架基础(02):Bean的生命周期,作用域,装配总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。