- //
- // Source code recreated from a .class file by IntelliJ IDEA
- // (powered by FernFlower decompiler)
- //
-
- package org.springframework.beans.factory.config;
-
- import org.springframework.beans.BeansException;
- import org.springframework.lang.Nullable;
-
- public interface BeanPostProcessor {
- @Nullable
- default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- return bean;
- }
-
- @Nullable
- default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- return bean;
- }
- }
- package com.example.PostProcessor;
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
-
- public class MyBeanPostProcessor implements BeanPostProcessor {
- @Override
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- System.out.println(beanName + ":postProcessBeforeInitialization");
- return bean;
- }
-
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- System.out.println(beanName + ":postProcessAfterInitialization");
- return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
- }
- }
测试类代码
- package com.example.Test;
-
-
- import com.example.Service.Impl.UserServiceImpl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- System.out.println(context.getBean(UserServiceImpl.class));
- }
- }
运行结果如下

展示了该后处理器的执行时机
- package com.example.Service.Impl;
-
- import com.example.DAO.UserDAO;
- import com.example.Service.UserService;
- import org.springframework.beans.factory.InitializingBean;
-
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
-
-
- public class UserServiceImpl implements UserService, InitializingBean {
- // todo 无参构造方法
- public UserServiceImpl() {
- System.out.println("UserServiceImpl实例化");
- }
-
- // todo 自定义初始化方法
- public void init() {
- System.out.println("自定义初始化方法init()");
- }
-
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("属性设置之后执行afterPropertiesSet()");
- }
-
- }
- package com.example.PostProcessor;
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
-
- public class MyBeanPostProcessor implements BeanPostProcessor {
- @Override
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- System.out.println(beanName + ":postProcessBeforeInitialization");
- return bean;
- }
-
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- System.out.println(beanName + ":postProcessAfterInitialization");
- return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
- }
- }
- "1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
- <bean class="com.example.PostProcessor.MyBeanPostProcessor">bean>
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl" init-method="init">
- beans>
测试类
- package com.example.Test;
-
-
- import com.example.Service.Impl.UserServiceImpl;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- System.out.println(context.getBean(UserServiceImpl.class));
- }
- }
运行结果

- package com.example.PostProcessor;
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
-
- import java.lang.reflect.InvocationHandler;
- import java.lang.reflect.Proxy;
- import java.util.Date;
-
- public class TimeLogBeanPostProcessor implements BeanPostProcessor {
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- // todo 使用动态代理对目标bean进行增强,返回proxy对象,进而存储到单例池singletonObjects中
- Object beanProxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), (InvocationHandler) (proxy, method, args) -> {
- // 输出开始时间
- System.out.println("方法" + method.getName() + "开始执行时间" + new Date());
- // 执行目标方法
- Object rs = method.invoke(bean, args);
- // 输出结束时间
- System.out.println("方法" + method.getName() + "结束执行时间" + new Date());
- return rs;
- });
- return beanProxy;// 将增强的bean存入单例池中
- }
- }
-
bean对象对应的类
- package com.example.Service.Impl;
-
- import com.example.Service.UserService;
-
-
- public class UserServiceImpl implements UserService {
- public void show() {
- System.out.println("show......");
- }
-
- }
接口类
- package com.example.Service;
-
- public interface UserService {
- public void show();
- }
配置文件
- "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 class="com.example.PostProcessor.TimeLogBeanPostProcessor">bean>
- <bean id="userService" class="com.example.Service.Impl.UserServiceImpl">
- bean>
-
-
- beans>
测试类
- package com.example.Test;
-
-
- import com.example.Service.UserService;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class TestApplicationContext {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
- UserService UserServiceBean = (UserService) context.getBean(UserService.class);
- UserServiceBean.show();
- }
- }
-
运行结果




在学习本文章内容的时候,我也去补充了一下,基础知识,便于更加程序运行原理,具体文章如下
Java高级-代理(proxy)_熵240的博客-CSDN博客
Java高级-注解_熵240的博客-CSDN博客
反射的作用、应用场景_熵240的博客-CSDN博客
Java高级-反射_熵240的博客-CSDN博客
Lambda表达式_熵240的博客-CSDN博客