转:http://blog.csdn.net/zhjb1025/article/details/3244728
Spring 的几个接口
1.InitializingBean接口,在初始化Bean时容器会调用前者的afterPropertiesSet()方法
2.DisposableBean接口,在析构Bean时容器会调用destroy()方法,在下面的例子中好像没有体现出来(革命尚未成功,同志仍需努力)
3.BeanFactoryAware接口,当它被BeanFactory创建后,它会拥有一个指向创建它的BeanFactory的引用
4.BeanPostProcessor接口,这个接口两个方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName) 在其他Bean构造前后执行
5.BeanFactoryPostProcessor接口,Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它
下面来看一下例子
-
package com.test;
-
-
public class User {
-
private String name;
-
-
public String getName() {
-
return name;
- }
-
-
public void setName(String name) {
-
System.out.println("User类 setName方法");
-
this.name = name;
- }
-
public void init() throws Exception {
-
System.out.println("User类init 方法");
-
- }
-
public User(){
-
System.out.println("User类构造方法");
- }
-
- }
-
-
package com.test;
-
-
import org.springframework.beans.BeansException;
-
import org.springframework.beans.factory.BeanFactory;
-
import org.springframework.beans.factory.BeanFactoryAware;
-
import org.springframework.beans.factory.DisposableBean;
-
import org.springframework.beans.factory.InitializingBean;
-
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
-
import org.springframework.beans.factory.config.BeanPostProcessor;
-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
-
-
public class JavaBean implements InitializingBean,DisposableBean,BeanFactoryAware,BeanPostProcessor ,BeanFactoryPostProcessor{
-
private String name;
-
public JavaBean(){
-
System.out.println("JavaBean类构造方法");
- }
-
@Override
-
public void afterPropertiesSet() throws Exception {
-
System.out.println("InitializingBean 接口 afterPropertiesSet方法");
-
- }
-
-
@Override
-
public void destroy() throws Exception {
-
System.out.println("DisposableBean 接口 destroy方法");
-
- }
-
public void init() throws Exception {
-
System.out.println("JavaBean类init 方法");
-
- }
-
-
@Override
-
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
-
System.out.println("BeanFactoryAware 接口 setBeanFactory方法");
-
- }
-
-
@Override
-
public Object postProcessAfterInitialization(Object bean, String beanName)
-
throws BeansException {
-
System.out.println(beanName+":BeanPostProcessor 接口 postProcessAfterInitialization方法");
-
return bean;
- }
-
-
@Override
-
public Object postProcessBeforeInitialization(Object bean, String beanName)
-
throws BeansException {
-
System.out.println(beanName+":BeanPostProcessor 接口 postProcessBeforeInitialization 方法");
-
return bean;
- }
-
-
public String getName() {
-
return name;
- }
-
-
public void setName(String name) {
-
System.out.println("JavaBean类setName 方法");
-
this.name = name;
- }
-
@Override
-
public void postProcessBeanFactory(
-
ConfigurableListableBeanFactory beanFactory) throws BeansException {
-
System.out.println("BeanFactoryPostProcessor 接口 postProcessBeanFactory 方法");
- }
-
-
- }
-
-
package com.test;
-
-
import org.springframework.context.ApplicationContext;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-
public class Test {
-
-
-
public static void main(String[] args) {
-
System.out.println("加载Spring配置文件");
- ApplicationContext context =
-
new ClassPathXmlApplicationContext("spring.xml");
-
System.out.println("加载Spring配置文件结束");
-
JavaBean bean=(JavaBean)context.getBean("javaBean");
-
System.out.println("获取name属性:"+bean.getName());
-
System.out.println("程序结束");
- }
-
- }
-
Spring配置文件
-
<?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
-
<beans>
-
<bean id="javaBean" class="com.test.JavaBean" init-method="init">
-
<property name="name" value="zhjb"></property>
-
</bean>
-
<bean id="user" class="com.test.User" init-method="init">
-
<property name="name" value="zhjb" ></property>
-
</bean>
-
</beans>
运行结果如下:
加载Spring配置文件
JavaBean类构造方法
JavaBean类setName 方法
BeanFactoryAware 接口 setBeanFactory方法
InitializingBean 接口 afterPropertiesSet方法
JavaBean类init 方法
BeanFactoryPostProcessor 接口 postProcessBeanFactory 方法
User类构造方法
User类 setName方法
user:BeanPostProcessor 接口 postProcessBeforeInitialization 方法
User类init 方法
user:BeanPostProcessor 接口 postProcessAfterInitialization方法
加载Spring配置文件结束
获取name属性:zhjb
程序结束
从结果进行分析,Bean的初始化顺序应该是
1.构造函数
2.初始化属性
3.如果实现了BeanFactoryAware 接口执行setBeanFactory方法
4..如果实现了InitializingBean 接口执行afterPropertiesSet方法
5.如果在配置文件中指定了init-method,那么执行该方法
6..如果实现了BeanFactoryPostProcessor 接口在 “new”其他类之前执行 postProcessBeanFactory 方法(通过这个方法可以改变配置文件里面的属性值的配置)
7.如果实现了BeanFactoryPostProcessor 接口,那么会在其他bean初始化方法之前执行postProcessBeforeInitialization 方法,之后执行postProcessAfterInitialization方法
感觉奇怪的地方就是没有执行destroy方法,目前还不知道原因在什么地方
分享到:
相关推荐
Spring Bean创建初始化流程
本篇文章主要介绍了Spring 中如何控制2个bean中的初始化顺序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
3、通过spring的xml bean配置或bean注解指定初始化方法,如下面实例的initMethod方法通过@bean注解指定。 销毁的时候实现的方法 1、通过java提供的@PreDestroy注释; 2、通过实现spring提供的DisposableBean接口,并...
springBean加载过程源码解析文档,附有代码类名和行数
主要介绍了Spring Bean的初始化和销毁,结合实例形式详细分析了Spring Bean的初始化和销毁相关配置、使用方法及操作注意事项,需要的朋友可以参考下
spring的初始化,可以让初学者了解soring的开发过程,让开发轻松进入开发大门。
Spring bean 一般通过配置文件和注解进行加载,如果要实现jar或class...测试示例中是spring boot 的部分代码,动态加载的内容为接口实现类,且初始化时加载本地的实现类,动态加载后改为非程序加载目录中的jar实现类。
初始化后可访问Spring管理的Bean
下面小编就为大家带来一篇浅谈spring容器中bean的初始化。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring源码学习六:bean初始化1
本篇将对定义在 XMl 文件中的 bean,从静态的的定义到变成可以使用的对象的过程,即 bean 的加载和获取的过程进行一个整体的了解
主要介绍了Spring Bean初始化及销毁多种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring 源码分析(Bean的初始化) 前言 本篇文章是个人第一次看spring源码并总结,同时也参考了下面这篇博客。基本也是按照他的思路来理解的。这也算是第一版个人简易理解。也算是窥见spring的冰山一角,之后也会...
NULL 博文链接:https://bijian1013.iteye.com/blog/2374256
只需要把需要注册进容器的bean声明为@Component即可,spring会自动扫描到这个Bean完成初始化并加载到spring上下文容器。 而当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成...
为了便于这些工作的设计,Spring IoC 容器提供了相关的功能,可以让应用定制 Bean 的初始化和销毁过程。 Bean 生命周期 的 init-method 属性或 @Bean 注解的 initMethod 属性),那么将调用该方法。 如果存在与 ...
Spring 延迟实例化
【课程介绍】:Spring启动时读取应用提供的Bean配置信息,在Spring容器中生成对应的Bean配置注册表...从源码证明springbean的实例化过程.mp44.spring的核心getSingleton方法.mp45.springbean的生成与循环依赖.mp46.getB
附件是在Spring Boot项目中使用CommandLineRunner接口来执行一些资源初始化的演示,该文档也详细的介绍了在@Component注解的类中可以注入@Controller,并把获取的数据存放到其他Bean类中的私有静态变量,算是对自己...