您好,登錄后才能下訂單哦!
1.以ApplocationContext上下文單例模式裝配bean為例,深入探討bean的生命周期:
(1).生命周期圖:
(2).具體事例:
person類實(shí)現(xiàn)BeanNameAware,BeanFactoryAware接口
public class Person implements BeanNameAware ,BeanFactoryAware{ private String name; public Person(){ System.out.println("調(diào)用構(gòu)造器為屬性值初始化"); } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void setBeanName(String arg0) { // TODO Auto-generated method stub System.out.println("獲取beanName id值"+" "+arg0); } @Override public void setBeanFactory(BeanFactory arg0) throws BeansException { // TODO Auto-generated method stub System.out.println("獲取BeanFactory" +" "+arg0); } }
public class MyBeanPostProcessor implements BeanPostProcessor{ @Override public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("調(diào)用postProcessAfterInitialization"); return arg0; } @Override public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("調(diào)用postProcessBeforeInitialization"); return arg0; } }
ApplicationContext.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- bean的配置文件 --> <bean id="person" class="org.jingdong.bean.life.Person"> <property name="name" value="grl"></property> </bean> <bean id="myBeanPostProcessor" class="org.jingdong.bean.life.MyBeanPostProcessor"></bean> </beans>
Main.java
public class Main { public static void main(String[] args) { // 創(chuàng)建IOC容器 ApplicationContext ac = new ClassPathXmlApplicationContext("org/jingdong/bean/life/applicationContext.xml"); //從容器中獲取bean實(shí)例 Person person = (Person) ac.getBean("person"); //使用bean System.out.println(person.getName()); } }
2.以Spring Factory裝配bean為例:
(1).生命周期圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。