第三章 IoC 和 AOP

3.1 IoC概述

3.1.1 通过实例理解 IoC 的概念
3.1.2 IoC 的类型
3.1.3 通过容器完成依赖关系的注入

3.2 相关 Java 基础知识

3.2.1 简单实例
package com.yly.demo02.entity;

public class Car {
	private String brand;
	private String color;
	private int maxSpeed;
	
	public Car() {}
	
	public Car(String brand, String color, int maxSpeed) {
		this.brand = brand;
		this.color = color;
		this.maxSpeed = maxSpeed;
	}
	
	public void introduce() {
		System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" + maxSpeed);
	}
   // 省略参数的getter/Setter 方法
}
  • 一般情况,创建Car的实例:
	Car car = new Car();
	car.setBrand("红旗CA72");
	或者
	Car car = new Car("红旗CA72","黑色");
  • 通过Java反射机制 间接操作目标类
package com.yly.demo02.text;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import com.yly.demo02.entity.Car;

public class ReflectTest {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Car initByDefaultConst() throws Exception {
		//@1 通过类装载器获取Car类对象
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		Class clazz = loader.loadClass("com.yly.demo02.entity.Car");
		
		//@2 获取类的默认构造器对象并通过它实例化Car
		Constructor[] cons = clazz.getDeclaredConstructors();
		System.out.println(cons);
		Car car = (Car) cons[0].newInstance();
		
		//@3 通过反射方法设置属性
		Method setBrand = clazz.getMethod("setBrand", String.class);
		setBrand.invoke(car, "红旗CA72");
		Method setColor = clazz.getMethod("setColor", String.class);
		setColor.invoke(car, "黑色");
		Method setMaxSpeed = clazz.getMethod("setMaxSpeed", int.class);
		setMaxSpeed.invoke(car, 200);
		
		return car;
	}
	
	//测试反射机制
	public static void main(String[] args) throws Exception {
		Car car = initByDefaultConst();
		car.introduce();
	}
}
3.2.2 类装载器ClassLoader
  • 类装载器工作机制(寻找类的字节码文件并构造出类再JVM内部标识的对象组件。)

    • 在Java中,类装载器把一个类装入JVM中,需要如下步骤
    • 1、装载:查找和导入Class文件;
    • 2、链接:执行校验、准备和解析步骤,
      • a) 校验:检查载入Class文件数据的正确性
      • b) 准备:给类的静态变量分配存储空间
      • c) 解析:将符号引用转成直接引用
    • 3、初始化:对类的静态变量、静态代码块执行初始化工作
  • 类装载工作由ClassLoader及其子类负责,

    • classLoader 是一个重要的Java运行时系统组件,负责在运行时查找和装入Class字节码文件。
    • JVM在运行时会产生三个ClassLoader:
    • 跟装载器: 非ClassLoader的子类,负责装载JRE的核心类库
    • ExtClassLoader(扩展类装载器):是ClasssLoader的子类,负责装载JRE扩展目录ext中的JAR类包
    • AppClassLoader(系统类装载器):是ClasssLoader的子类,负责装载ClassPath路径下的类包
package com.yly.demo02.text;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import com.yly.demo02.entity.Car;

public class ClassLoaderTest {

	/**
	 * @date 2019年12月14日 上午10:28:05
	 */
	public static void main(String[] args) {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		System.out.println("current loader:   " + loader);
		System.out.println("parent loader:    " + loader.getParent());
		System.out.println("grandparent loader:    " + loader.getParent().getParent());
	}
}
  • 总结 ClassLoader 是 AppClasssLoader, 父 ClassLoader 是 ExtClassLoader, 祖父 ClassLoader 是根类加载器,因为在java中无法获得它的句柄,所以直接返回null。

  • ClassLoader 重要方法

    • Class loadClass(String name) 参数指定类装载器需要装载类的名字,必须使用限定类名,如com.yly.demo02.entity.Car
    • Class defineClass(String name, byte[] b, int off, int len) 将类文件的字节数组转化成内部的JVM内部的Java.lang.Class 对象。字节数组可以从本地文件系统,远程网络获取。name为字节数据对应的全限定类名
    • Class findSystemClass(String name) 从本地文件系统载入Class 文件,如果本地文件系统不存在该Class 文件,将抛出ClassNotFoundException 异常。该方法是JVM默认使用的装载机制。
    • Class findLoadedClass(String name) 调用该方法来查看 ClassLoader 是否已装入某个类。如果已装入,那么返回java.lang.Class 对象,否则返回 null 。如果强行装载已存在的类,将会抛出链接错误。
    • ClassLoader getParent() 获取类装载的父装载器,除根装载器外,所有的类装载器都有且仅有一个父装载器,ExtClassLoader 的父装载器是根装载器,因为根装载器非Java编写,所以无法获取,将返回null。
3.2.3 Java 反射机制
  • Constructor 类的构造函数反射类,通过Class#getConstructors()方法可以获得类的所有构造函数反射对象数组。
  • Method 类方法的反射类 通过Class#getDeclaredMethods()方法可以获取类的所有方法反射类对象数组 Method[]。
    1. Class getReturnType() 获取方法的返回值类型
    2. Class[] getParameterTypes() 获取方法的入参类型数组
    3. Class[] getExceptionTypes() 获取方法的异常类型数组
    4. Annotation[][] getParameterAnnotations() 获取方法的注解信息
  • FieId 类的成员变量的反射类,通过Class#getDeclaredFileIds()方法可以获取类的成员变量反射对象数组,通过Class#getDeclaredField(String name)则可获取某个特定名称的成员变量反射对象。FileId类最主要的方法是set(Object obj,Object value), obj表示操作的目标对象,通过value为目标对象的成员变量设置值。
package com.yly.demo02.text;

public class PrivateCar {
	private String color;
	
	@SuppressWarnings("unused")
	private void deive() {
		System.out.println("drive private car! the color is:  " + color);
	}
}
package com.yly.demo02.text;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class PrivateCarReflect {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) throws Exception {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		Class clazz = loader.loadClass("com.yly.demo02.text.PrivateCar");
		PrivateCar pcar = (PrivateCar) clazz.newInstance();
		
		Field colorFId = clazz.getDeclaredField("color");
		// 取消Java语言访问检查以访问private变量
		colorFId.setAccessible(true);
		colorFId.set(pcar, "红色");
		
		Method driveMtd = clazz.getDeclaredMethod("deive", (Class[])null);
		
		driveMtd.setAccessible(true);
		driveMtd.invoke(pcar, (Object[])null);
		
	}	
}

3.3 资源访问利器

3.3.1 资源抽象接口

Resource接口主要方法

  • boolean exists() 资源是否存在
  • boolean isOpen() 资源是否打开
  • URL getURL() theows IOException 如果底层资源可以表示成URL,该方法返回对应的URL对象
  • File getFile() theows IOException 如果底层资源对应一个文件,该方法返回对应的File对象
  • InputStream getInputStream() throws IOException 返回资源对应的输入流
    Resource 具体实现类
  • ByteArrayResource 二进制数组表示的资源,二进制数组资源可以在内存中通过程序构造
  • ClassPathResource 类路径下的资源,资源以相对于类路径的方式表示,
  • FileSystemResource 文件系统资源,资源以文件系统路径的方式表示,如 D:/comf/bean.xml
  • InputStreamResource 对应一个InputStream 的资源
  • ServletContextResource 为访问Web容器上下文中的资源二设计的类,负责以相对于Web应用根目录的路径加载资源,它支持以流和URL 的方式访问,
  • UrlResource 封装了Java.net.URL,它使用户能够访问任何可以通过URL表示的资源
package com.yly.demo02.text;

import java.io.IOException;
import java.io.InputStream;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class FileSourceExample {

	public static void main(String[] args) {
		try {	
			String filePath = "E:/bj/MyEclipse/workpack/spring_ioc/src/main/resources/beans.xml";
			//@1 使用系统文件路径方式加载文件
			Resource res1 = new FileSystemResource(filePath);
			//@2 使用类路径方式加载文件
			Resource res2 = new ClassPathResource("beans.xml");
			
			InputStream ins1 = res1.getInputStream();
			InputStream ins2 = res2.getInputStream();
			
			System.out.println("res1:  " + res1.getFilename() + " " + ins1);
			System.out.println("res2:  " + res2.getFilename() + " " + ins2);
			
			
			Resource res = new ClassPathResource("beans.xml");
			EncodedResource encRes = new EncodedResource(res, "UTF-8");
			String context = FileCopyUtils.copyToString(encRes.getReader());
			System.out.println(context);	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
  • resource.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<jsp:directive.page import="org.springframework.web.context.support.ServletContextResource"></jsp:directive.page>
<jsp:directive.page import="org.springframework.core.io.Resource"></jsp:directive.page>
<jsp:directive.page import="org.springframework.web.util.WebUtils"></jsp:directive.page>

<%
	Resource res3 = new ServletContextResource(application,"/WEB-INF/classes/conf/flie1.txt");
	out.print(res3.getFilename() + "<br/>");
	out.print(WebUtils.getTempDir(application).getAbsolutePath());
 %>
3.3.2资源加载
地址前缀示例对应资源类型
classpath:classpath: com/baobaotao/beanfactory/beans.xml从类路径加载资源,classpath:和 classpath:/ 是等价的,都是相对于类的根路径。资源文件可以在标准的文件系统中,也可以在jar或zip的包中
file:file: /conf/com/baobaotao/beanfactory/beans.xml使用 UrlResource 从文件系统目录中装载资源,可采用绝对的相对路径
http://http:// www.baobaotao.com/resource/beans.xml使用 UrlResource 从Web服务起中装载资源
ftp://ftp:// www.baobaotao.com/resource/beans.xml使用 UrlResource 从FTP 服务器中装载资源
没有前缀com/baobaotao./beanfactory/beans.xml根据ApplicationContext 具体实现类采用对应的类型的Resource
  • 若由多个jar包或文件系统类路径都拥有相同的包名(com.yly)
    • classpath: 只会在第一个加载的com.yly包下查找
    • classpath*: 在所有这些jar包及类路径下查找
  • Ant 风格资源地址支持3种匹配符:
    • ?: 匹配文件名中的一个字符
    • *: 匹配文件名中任意个字符
    • **: 匹配多层路径
  • Ant风格的资源路径示例
    • classpath:com/t?st.xml 匹配 com 类路径下 com/text.xml, com/tast.xml 或者 com/txst.xml
    • file:D:/conf/*.xml 匹配文件系统 D:/com 目录下所有以 xml 为后缀的文件
    • classpath:com/**/text.xml 匹配 com 类路径下(当前目录及子孙目录)的text.xml文件
    • classpath:org/springframework/**/*.xml 匹配类路径 org/springframework 下所有以 xml 为后缀的文件
    • classpath:org/**/servlet/bla.xml 匹配类路径org/springframework/servlet/bla.xml, 也匹配 org/springframework/testing/servlet/bla.xml, 还匹配 org/servlet/bla.xml
package com.yly.demo02.text;

import java.io.IOException;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

public class patternResolverTest {

	public static void main(String[] args) throws IOException {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource resources[] = resolver.getResource("classpath*:com/yly/**/.xml");
		for (Resource resource : resources) {
			System.out.println(resource.getDescription());
		}
	}
}

3.4 BeanFactory 和 ApplicationContext

3.4.1 BeanFactory 介绍
  • BeanFactory 是一个类工厂,但和传统的类工厂不同,传统的类工厂仅负责构造一个或几个类的实例。而BeanFactory 是类的同用工厂,它可以创建并管理各种类的对象。这些可被创建和管理的对象本身没有什么特别之处,仅是一个POJO, Spring 称这些被创建和管理的Java对象为Bean。

  • Spring 为 BeanFactory提供了多种实现,最常用的是XmlBeanFactor

  • 涉及的其他接口分别说明

    • ListableBeanFactory 该接口定义了访问容器中Bean 基本信息的若干方法,如查看Bean 的个数、获取某一类型Bean 的配置名、查看容器中是否包括某一Bean 等方法
    • HierarchicalBeanFactory 父子级联IoC容器的接口,子容器可以通过接口方法访问父容器
    • ConfigurableBeanFactory 增强了IoC容器的可定制性,它定义了设置类装载器、属性编辑器、容器初始化后置处理器等方法,
    • AutowireCapableBeanFactory 定义了将容器中的Bean 按某种规则(如名字、类型匹配)进行自动装配
    • SingletonBeanRegistry 允许在运行期间向容器注册单实例Bean 的方法
    • BeanDefinitionRegistry 描述了Bean 的配置信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:util="http://www.springframework.org/schema/util"
	 xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	<bean id="car1" class="com.yly.demo02.entity.Car">
		<property name="brand" value="红旗CA72U"></property>
		<property name="color" value="黑色U"></property>
		<property name="maxSpeed" value="200"></property>
	</bean>
</beans>
  • 通过 XmlBeanFactory 实现类启动 SpringIoC容器
package com.yly.demo02.text;

import java.io.IOException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.yly.demo02.entity.Car;

public class BeanFactoryTest {

	public static void main(String[] args) throws IOException {
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		
		Resource[] res = resolver.getResources("classpath:beans.xml");
		BeanFactory bf = new XmlBeanFactory(res[0]);
		System.out.println(bf);
		
		Car car = bf.getBean("car1", Car.class);
		System.out.println(car.getBrand() + "  " + car.getColor() + "  " + car.getMaxSpeed());
		System.out.println("car bean is ready fro use");
	}
}
3.4.2 ApplicationContext 介绍
  • ApplicationContext 的主要实现类是 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext, 前者默认从类路径加载配置文件,后者默认从文件系统中装配文件
  • ApplicationContext 继承了 HierarchicalBeanFactory 和 ListableBeanFactory 接口,可以通过多个其他接口扩展 BeanFactory 的功能,接口如下
  • ApplicationEventPublisher 让容器拥有发布应用上下文事件的功能,包括容器启动、关闭时间,并对事件进行响应处理。
  • MessageSource 为应用提供 i18n 国际化消息访问的功能
  • ResourcePatternResolver 所有 ApplicationContext 实现类都实现了类似于PathMatchingResourcePatternResolver的功能,可以通过带前缀的 Ant 风格的资源文件路径装载 Spring 的配置文件
  • LifeCycle 该接口提供了 start() 和 stop() 两个方法,主要用于控制异步处理过程。

基于类注解的配置方法

package com.yly.applicationContext;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.yly.demo02.entity.Car;

@Configuration
public class Beans {
	
	@Bean(name = "car2")
	public Car buildCar() {
		Car car = new Car();
		car.setBrand("红旗CA72U");
		car.setColor("军绿");
		car.setMaxSpeed(200);
		return car;
	}

	public static void main(String[] args) {
		//通过一个带@Configuration 的POJO装载Bean配置
		ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
		Car car = ctx.getBean("car2",Car.class);
		System.out.println(car.getBrand() + "  " + car.getColor() + "  " + car.getMaxSpeed());
	}
}
3.4.3 父子容器

3.5 Bean 的生命周期

3.5.1 BeanFactory 中 Bean的生命周期
  • 生命周期

    • 当调用者通过 getBean(beanName)向容器请求某一个 Bean 时,如果容器注册了org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor 接口,在实例化 Bean 之前, 将调用接口的 postProcessBeforeInstantiation() 方法;
    • 根据配置情况调用接口的 Bean 构造函数或工厂方法实例化 Bean;
    • 如果容器注册了 InstantiationAwareBeanPostProcesseor 接口,在实例化 Bean 之后,调用该接口的 postProcessAfterInstantiation() 方法,可以在这里对已经实例化的对象进行一些“梳妆打扮”
    • 如果 Bean 配置了属性信息,容器在这一步着手将配置值设置到 Bean 对应的属性中,不过在设置每个属性之前将先调用 InstantiationAwareBeanPostProcessor 接口的 postProcessPropertyValue() 方法;
    • 调用 Bean 的属性设置方法设置属性值;
    • 如果 Bean 实现了 org.springframework.beans.factory.BeanNameAware 接口,将调用setBeanName() 接口方法,将配置文件中该 Bean 对应的名称设置到 Bean 中;
    • 如果 Bean 实现了 org.springframework.beans.factory.BeanFactoryAware 接口,将用 setBeanFactory() 接口方法 , 将 BeanFactory 容器实例设置到 Bean 中;
    • 如果 BeanFactory 装配了 org.springframework.beans.factory.config.BeanPostProcessor 后处理器,将调用 BeanPostProcessor 的 Object postProcessBeforeInitialization(Object bean, String beanName) 接口方法对 Bean 进行加工操作。其中入参 bean 正是当前正在处理的 Bean, 而beanName 正是当前 Bean 的配置名,返回的对象为加工后处理的Bean。用户可以使用该方法对某些 Bean 进行特殊的处理,甚至改变 Bean 的行为, BeanPostProcessor 在Spring框架中占有重要的地位,为容器提供对 Bean 进行后续加工处理的切入点,Spring容器所提供的各种‘神奇功能’(如AOP, 动态代理等) 都通过BeanPostProcessor实施
    • 如果 Bean 实现了 InitializeingBean 的接口,将调用接口的 afterPropertiesSet() 方法;
    • 如果在< bean > 通过 init-method 属性定义了初始化方法,将执行这个方法;
    • BeanPostProcessor 后处理器定义了两个方法:其一是postProcessBeforeInitialization()在第8步调用;其二是Object postProcessAfterinitialization(Object bean, String beanName)方法,这个方法在此时调用,容器再次获得对 Bean 进行加工处理的机会
    • 如果在< bean > 中指定 Bean 的作用范围为 scope=“prototype”,将 Bean 返回给调用者,调用者负责Bean后续生命的管理,Spring不在管理这个Bean的生命周期。如果作用范围设置为scope=“singleton”,则将Bean放入到Spring IoC容器的缓存池中,并将Bean引用返回给调用者,Spring继续对这些Bean经写后续的生命管理
    • 对于scope="singleton"的Bean, 当容器关闭时, 将触发Spring 对 Bean 的后续生命周期的管理工作,首先如果Bean实现了DIsposableBean接口,则将调用接口的afterPropertiesSet() 方法,可以在次编写释放资源、记录日志等操作
    • 对于scope="singleton"的Bean,如果通过< bean>的destroy-method属性指定了Bean的销毁方法,Spring将执行Bean的这个方法,完成Bean资源的释放等操作
  • Bean的生命周期大致划分的三类

    • Bean 自身的方法:如调用Bean 构造函数实例化Bean, 调用Setting设置 Band的属性以及通过< bean> 的 init-method 和 destroy-method所指的方法;
    • Bean 级生命周期接口方法: 如BeanNameAware、BeanFactoryAware、InitializingBean 和 DisPosaBleBean,这些接口方法由 Bean类之间实现
    • 容器级生命周期接口方法:由InstantiationAwareBeanPostProcessor和BeanPostProcessor这两个接口实现,一般称他们的实现类为"后处理器"。后处理器接口一般不由Bean 本身实现,他们独立于 Bean, 实现类以容器附加装置的形式注册到Spring 容器所创建任何Bean 的时候,这些后处理器都会发生作用,所以这些后处理器的影响是全局性的。当然用户可以通过合理的编写后处理器,让其仅对感兴趣Bean进行加工。
package com.yly.BeanFaction.entity;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

//@1 管理Bean生命周期接口
public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean{
	private String brand;
	private String color;
	private int maxSpeed;
	
	private BeanFactory beanFactory;
	private String beanName;
	
	public Car() {
		System.out.println("调用Car()构造函数");
	}
	
	public void setBrand(String brand) {
		System.out.println("调用setBrand()设置属性");
		this.brand = brand;
	}
	
	public void introduce() {
		System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:" + maxSpeed);
	}

	//@2 BeanFactoryAware接口方法
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("调用BeanFactoryAware.setBeanFactory");
		this.beanFactory = beanFactory;
	}

	//@3 BeanNameAware接口方法
	@Override
	public void setBeanName(String name) {
		System.out.println("调用BeanFactoryAware.setBeanName()");
		this.beanName = beanName;
	}
	
	//@4 InitializingBean接口方法
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("调用InitializingBean.afterPropertiesSet()");
	}
	
	//@5 DisposableBean 接口方法
	@Override
	public void destroy() throws Exception {
		System.out.println("调用DisposableBean.destroy()");
	}

	//@6 通过 <bean> 的init-method属性指定的初始化方法
	public void myInit() {
		System.out.println("调用init-method所指定的myInit(),将maxSpeed设置为240");
		this.maxSpeed = 240;
	}
	
	//@7 通过<bean>的destroy-method属性指定的销毁方法
	public void myDestroy() {
		System.out.println("调用destroy-method所指定的myDestroy()");
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getMaxSpeed() {
		return maxSpeed;
	}

	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}

	public String getBrand() {
		return brand;
	}

	public BeanFactory getBeanFactory() {
		return beanFactory;
	}

	public String getBeanName() {
		return beanName;
	}

	@Override
	public String toString() {
		return "Car [brand=" + brand + ", color=" + color + ", maxSpeed=" + maxSpeed + ", beanFactory=" + beanFactory
				+ ", beanName=" + beanName + "]";
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	
	<bean id="car" class="com.yly.BeanFaction.entity.Car" 
		init-method="myInit" 
		destroy-method="myDestroy" 
		p:brand="红旗CA72"
		p:maxSpeed="200"/>
</beans>
package com.yly.BeanFaction.faction;

import java.beans.PropertyDescriptor;

import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;

public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter{
	
	//@1 接口方法: 在实例化Bean前进行调用
	public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
		//@1-1 仅对容器中car Bean进行处理
		if("car".equals(beanName)) {
			System.out.println(" InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
		}
		return null;
	}
	
	//@2 接口方法: 在实例Bean后调用
	public boolean postProcessAfterInstantiation(Object bean, String beanName) {
		//@2-1 仅对容器中car Bean进行处理
		if("car".equals(beanName)) {
			System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation");
		}
		return true;
	}
	
	//@3 接口方法: 在设置某个属性时调用
	public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
		//@3-1 仅对容器中car Bean进行处理,还可以通过pdst入参进行过滤
		//仅对car的某个特定属性时进行处理
		if("car".equals(beanName)) {
			System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues");
		}
		return pvs;
	}	
}
package com.yly.BeanFaction.faction;

import org.springframework.beans.factory.config.BeanPostProcessor;

import com.yly.BeanFaction.entity.Car;

public class MyBeanPostProcessor implements BeanPostProcessor{

	public Object postProcessBeforeInitialization(Object bean, String beanName) {
		if(beanName.equals("car")) {
			Car car = (Car)bean;
			if(car.getColor()==null) {
				System.out.println("调用BeanPostProcess.postProcessBeforeInitialization(),color为空,设置为默认黑色");
				car.setColor("黑色");
			}
		}
		return bean;
	}
	
	public Object postProcessAfterInitialization(Object bean, String beanName) {
		if(beanName.equals("car")) {
			Car car = (Car)bean;
			if(car.getMaxSpeed() >= 200) {
				System.out.println("调用BeanPostProcess.postProcessAfterInitialization(),将maxSpeed调整为200");
				car.setMaxSpeed(200);
			}
		}
		return bean;
	}	
}
package com.yly.BeanFaction.faction;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.yly.BeanFaction.entity.Car;

public class BeanLifeCycle {

	private static void LifeCycleInBeanFactory() {
		//@1 下面两句装载配置文件并启动容器
		Resource res = new ClassPathResource("beans.xml");
		BeanFactory bf = new XmlBeanFactory(res);
		
		//@2 向容器中注册MyBeanPostProcessor后处理器
		((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyBeanPostProcessor());
		
		//@3 向容器中注册MyInstantiationAwareBeanPostProcessor后置处理
		((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
		
		//@4 第一次从容器中获取Car,将触发容器实例化Bean,这将引发Bean生命周期方法的调用
		Car car1 = (Car) bf.getBean("car");
		car1.introduce();
		car1.setColor("红色");
		
		//@5 第二次从容器中获取car,直接从缓存池中获取
//		Car car2 = (Car) bf.getBean("car");
		
		//@6 查看car1和car2是否指向统一引用
//		System.out.println("car1 == car2:" + (car1 == car2));
		
		//@7 关闭容器
//		((XmlBeanFactory)bf).destroySingletons();
	}
	
	public static void main(String[] args) {
		LifeCycleInBeanFactory();
	}	
}
  • 测试结果打印
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
调用Car()构造函数
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
InstantiationAwareBeanPostProcessor.postProcessPropertyValues
调用setBrand()设置属性
调用BeanFactoryAware.setBeanName()
调用BeanFactoryAware.setBeanFactory
调用BeanPostProcess.postProcessBeforeInitialization(),color为空,设置为默认黑色
调用InitializingBean.afterPropertiesSet()
调用init-method所指定的myInit(),将maxSpeed设置为240
调用BeanPostProcess.postProcessAfterInitialization(),将maxSpeed调整为200
brand:红旗CA72;color:黑色;maxSpeed:200
car1 == car2:true
调用DisposableBean.destroy()
调用destroy-method所指定的myDestroy()
3.5.2 ApplicationContext 中 Bean 的生命周期
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值