-
Notifications
You must be signed in to change notification settings - Fork 20
Spring容器扩展点之BeanPostProcessor
目录 start
目录 end|2020-04-27 23:42|
在Spring中。我们可以定义bean的初始化方法,从而完成某些初始化动作。但当我们需要在bean的初始化之前或之后完成某些操作该怎么办呢?对于优秀的Spring,当然已经想到了这一点,那便就是我们今天的主角BeanPostProcessor接口了。
那么什么是BeanPostProcessor呢,他怎么使用呢?首先让我们来看下源码中对该接口的解释吧!

该接口的注释大意是这样的
工厂钩子,允许自定义修改新的bean实例,例如 检查标记接口或用代理包装它们。
ApplicationContexts可以在其bean定义中自动检测 BeanPostProcessor bean,并将它们应用于随后创建的任何bean。bean factories允许对后处理器进行编程注册,适用于通过该工厂创建的所有bean。
简单来说,就是我们可以在Spring创建bean实例后,bean初始化之前和初始化之后完成一些自定义的操作。
然后让我们来看看它的两个方法:
-
postProcessBeforeInitialization -
postProcessAfterInitialization
顾名思义,这两个方法,一个是在bean初始化之前执行,一个是在bean初始化之后执行。
假如有个定义好的Student,现在希望在不改变原有代码的情况下将它的address字段赋上某个值。
- Student
@Component
@Data
public class Student {
private int id;
private String name;
private String address;
}- 扩展
@Component
public class StudentExpansion implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Student) {
Student student = (Student) bean;
student.setAddress("中国");
}
return bean;
}
}-
【 Algorithm 】
-
【 Blog 】
-
【 C 】
-
【 Database 】
-
【 Distributed 】
-
【 FrontEnd 】
- 【 FrontEnd/Frame 】
- 【 FrontEnd/Node 】
- Font
- Hexo
- JavaScript
- LearnPS
- ResponseCode
- SVG
- ViewSolution
- extjs学习笔记
-
【 Functional 】
-
【 Go 】
-
【 Groovy 】
-
【 Java 】
- 【 Java/AdvancedLearning 】
- 【 JavaBasic 】
- 【 JavaCache 】
- 【 JavaCollection 】
- 【 JavaConcurrency 】
- 【 JavaMap 】
- Annotation
- ClassFile
- Collection
- Concurrency
- Deploy
- Exception
- ExtendsAndInterface
- Generics
- IO
- JDBC
- JDKAndJRE
- JMX
- JVM
- Java11
- Java7
- Java8
- JavaNetwork
- JavaReleaseVersion
- JavaWeb
- JvmPerformance
- MQ
- MultipleLanguage
- Proxy
- Reflection
- Serialize
- SyntaxAndType
- Thread
- WebPerformance
- 【 Java/Android 】
- 【 Java/Ecosystem 】
- 【 Java/MSA 】
- 【 Java/Spring 】
- 【 Java/TemplateEngine 】
- 【 Java/Test 】
- 【 Java/Tool 】
- 【 Java/thread 】
- AlibabaJavaStandard
- DesignPattern
- HashMap解析
- Java-NIO
- Java虚拟机
- Log
- MIS
- Quartz
- RESTful
- WebSocket学习笔记
- ZooKeeper学习笔记
- android学习笔记
- 【 Java/AdvancedLearning 】
-
【 Kotlin 】
-
【 Linux 】
- 【 Linux/Alpine 】
- 【 Linux/Arch 】
- 【 Linux/Base 】
- 【 Linux/Centos 】
- 【 Linux/Container 】
- 【 Linux/Debian 】
- 【 Linux/Tool 】
- JavaDevInit
- Linux系统学习
-
【 MyBlog 】
-
【 Python 】
- 【 Python/Tool 】
- Python
- PythonConcurrent
- PythonGUI
- PythonGame
- PythonNet
- PythonOffices
- PythonWeb
- Python基础
- Python核心学习
-
【 Reactive 】
-
【 Rust 】
-
【 Scala 】
-
【 Script 】
-
【 Skills 】
- 【 Skills/Application 】
- 【 Skills/CS 】
- 【 Skills/Cache 】
- 【 Skills/Councurrency 】
- 【 Skills/DevOps 】
- 【 Skills/Document 】
- 【 Skills/Ecology 】
- 【 Skills/Network 】
- 【 Skills/Search 】
- 【 Skills/SoftwareEngineering 】
- 【 Skills/Spider 】
- 【 Skills/Test 】
- 【 Skills/Vcs 】
- 【 Skills/Work 】
- AppManual
- CelebrityQuotes
- Miscellaneous
- Platform
- Problem
- Protobuf
- RegularExpression
- SoftwareDesignEngineer
- Website
-
【 Windows 】