Spring框架中Bean的创建过程如下:
其中BeanPostProcessor接口包含两个执行过程,分别是在bean执行init方法之前和执行init方法之后执行。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| @Configuration public class MyBeanPostProcessor implements BeanPostProcessor {
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("我在bean初始化前执行"); return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName); }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("我在bean初始化之后执行"); return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); }
@Bean(name = "DBConnection", initMethod = "init", destroyMethod = "destroy") public DatabaseConfig mysqlConnection() { Properties properties = System.getProperties(); String url = properties.getProperty("url"); String driverName = properties.getProperty("driverName"); String userName = properties.getProperty("userName"); String password = properties.getProperty("password"); return new DatabaseConfig(driverName, url, userName, password); }
}
|
上述代码新建了自定义的MyBeanPostProcessor
,并实现了接口BeanPostProcessor
中的两个接口(自带default方法),同时,还定义了一个名为DBConnection
的bean,指定了初始化方法和销毁前执行的方法。
其中,DatabaseConfig
的定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @Data public class DatabaseConfig {
private static Connection connection;
private String driverName; private String url; private String userName; private String password;
public DatabaseConfig(String driverName, String url, String userName, String password) { this.driverName = driverName; this.url = url; this.userName = userName; this.password = password; System.out.println("构造连接参数..."); } public void init() { System.out.println("初始化数据库连接"); } public void destroy() { System.out.println("销毁数据库连接"); } }
|
为了方便识别执行阶段,分别在构造器和init
以及destroy
方法中加入了文字说明。
最后的执行结果如下:
1 2 3 4 5 6
| 构造连接参数... 我在bean初始化前执行 初始化数据库连接 我在bean初始化之后执行 ... 销毁数据库连接
|
由此可见,构造器内的代码是最先执行的,属于填充属性阶段(即Populate properties
),接着执行MyBeanPostProcessor#postProcessBeforeInitialization
方法,然后执行init方法
,之后执行MyBeanPostProcessor#postProcessBeforeInitialization
,最后在关闭应用时执行destroy
方法。