spring有关bean的加载问题

一、好言

掏心掏肺的对一个人好,要么得到一生的知己,要么换来一生的教训。

二、背景

最近离职在交接,所以有时间来在看看spring的一些加载及周期问题,自己写代码测试,看看源码之类的,最近用了ApplicationContextAware,BeanPostProcessor,慢慢熟悉用法,然后在跟源码看了。下面是一些示例。

三、代码示例

3.1 bean加载顺序

  1. 实例化;
  2. 设置属性值;
  3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;
  4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;
  5. 如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext
  6. 调用BeanPostProcessor的预先初始化方法;
  7. 调用InitializingBean的afterPropertiesSet()方法;
  8. 调用定制init-method方法;
  9. 调用BeanPostProcessor的后初始化方法;

摘自网上

3.2 bean的初始化

package com.mouse.moon.spring.bean;
import org.springframework.beans.factory.InitializingBean;
/**
 * Created by Mahone Wu on 2017/3/17.
 */
public class Man implements InitializingBean {


    private WoMan woMan;

    public Man(){
        System.out.println("初始化  man");
    }

    public void setWoMan(WoMan woMan){
        System.out.println("setWoMan");
        this.woMan = woMan;
    }

    public void init(){
        System.out.println("init");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet");
    }

}
package com.mouse.moon.spring.bean;
/**
 * Created by Mahone Wu on 2017/3/17.
 */
public class WoMan {
    public WoMan(){
        System.out.println("init... woman类");
    }

    /**说点啥了*/
    public String say(String str){
        return  str;
    }
}
package com.mouse.moon.spring.bean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
 * Created by Mahone Wu on 2017/3/17.
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/test/spring.xml");
        Man man = (Man)context.getBean("man");
    }
}

打印日志如下:

初始化  man
.17:42:12.463 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[529] - Eagerly caching bean 'man' to allow for resolving potential circular references
.17:42:12.465 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[221] - Creating shared instance of singleton bean 'woMan'
.17:42:12.465 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[448] - Creating instance of bean 'woMan'
init... woman类
.17:42:12.465 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[529] - Eagerly caching bean 'woMan' to allow for resolving potential circular references
.17:42:12.466 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[484] - Finished creating instance of bean 'woMan'
setWoMan
.17:42:12.521 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[1620] - Invoking afterPropertiesSet() on bean with name 'man'
afterPropertiesSet
.17:42:12.522 DEBUG [main] org.springframework.beans.factory.support.DefaultListableBeanFactory[1678] - Invoking init method  'init' on bean with name 'man'
init

执行的顺序:
构造方法
set方法
afterPropertiesSet
init-method方法配置

如果加上@PostConstruct,则需要在xml里面加上如下配置

<?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: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/context
       http://www.springframework.org/schema/context/spring-context.xsd">
       <context:annotation-config/>
       <bean id="man" class="com.mouse.moon.spring.bean.Man" init-method="init">
              <property name="woMan" ref="woMan"></property>
       </bean>
       <bean id="woMan" class="com.mouse.moon.spring.bean.WoMan"></bean>
</beans>

<context:annotationconfig />将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、 CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及 RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan阅读 4,220评论 2 7
  • 注意LifecycleProcessor接口继承了Lifcycle接口。同时,增加了2个方法,用于处理容器的ref...
    google666s阅读 1,125评论 0 51
  • 《冈仁波齐》,一部纪录片式的电影,演员是当地找的藏民,没有剧情安排,仅仅只是拍摄队伍跟着十一人的朝圣者完全还原的拍...
    薄稚阅读 1,252评论 0 1