博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring初始化Bean的过程
阅读量:6819 次
发布时间:2019-06-26

本文共 4717 字,大约阅读时间需要 15 分钟。

最近打算写一个spring-mvc的插件,便于做接口测试,既然是插件,那就是零耦合。知道spring有几个接口,BeanPostProcessor, InitializingBean, DisposableBean, ApplicationContextAware, BeanFactoryPostProcessor,这几个接口也涉及到bean的生命周期。

贴代码:
调用类:

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

27

28

29

30

31

public interface Person {

void eatMeat();

}

public class Student implements Person {

private Fork fork;

public Student() {

System.out.println("Student.Constructor");

}

public Fork getFork() {

return fork;

}

public void setFork(Fork fork) {

this.fork = fork;

System.out.println("Student.setFork");

}

public void eatMeat() {

this.fork.forkMeat();

System.out.println("Student.eatMeat");

}

public void init() {

System.out.println("Student.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

public interface Fork {

void forkMeat();

}

public class WoodFork implements Fork {

String name;

public String getName() {

return name;

}

public void setName(String name) {

System.out.println("WoodFork.setName");

this.name = name;

}

public WoodFork() {

System.out.println("WoodFork.Constructor");

}

@Override

public void forkMeat() {

System.out.println("WoodFork.forkMeat");

}

}

bean的几个接口实现类:

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

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

import org.springframework.beans.BeansException;

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;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

public class MyBeanPostProcessor implements BeanPostProcessor, InitializingBean, DisposableBean, ApplicationContextAware, BeanFactoryPostProcessor {

public String name;

public void setName(String name) {

this.name = name;

System.out.println("MyBeanPostProcessor.setName");

}

public MyBeanPostProcessor() {

System.out.println("MyBeanPostProcessor.Constructor");

}

public void setApplicationContext(ApplicationContext applicationContext)

throws BeansException {

System.out.println("MyBeanPostProcessor.setApplicationContext");

}

public Object postProcessBeforeInitialization(Object bean, String beanName)

throws BeansException {

System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization for " + bean.getClass().getSimpleName());

return bean;

}

public Object postProcessAfterInitialization(Object bean, String beanName)

throws BeansException {

System.out.println("MyBeanPostProcessor.postProcessAfterInitialization for " + bean.getClass().getSimpleName());

return bean;

}

public void destroy() throws Exception {

System.out.println("MyBeanPostProcessor.destory");

}

public void afterPropertiesSet() throws Exception {

System.out.println("MyBeanPostProcessor.afterPropertiesSet");

}

@Override

public void postProcessBeanFactory(

ConfigurableListableBeanFactory beanFactory) throws BeansException {

System.out.println("MyBeanPostProcessor.postProcessBeanFactory");

}

}

spring配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="" xmlns:xsi=""

xmlns:p="" xmlns:context=""

xmlns:mvc=""

xsi:schemaLocation=" " >

<bean id="woodFork" class="com.kaige.spring.WoodFork" >

<property name="name" value="wood"></property>

</bean>

<bean id="student" class="com.kaige.spring.Student" init-method="init">

<property name="fork" ref="woodFork"></property>

</bean>

<bean class="com.kaige.spring.MyBeanPostProcessor">

<property name="name" value="processor"></property>

</bean>

</beans>

测试:

1

2

3

4

5

6

7

8

9

10

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");

Student s = (Student)ctx.getBean("student");

s.eatMeat();

ctx.registerShutdownHook();

System.out.println("end");

}

}

结果:

MyBeanPostProcessor.Constructor
MyBeanPostProcessor.setName
MyBeanPostProcessor.setApplicationContext
MyBeanPostProcessor.afterPropertiesSet
MyBeanPostProcessor.postProcessBeanFactory
WoodFork.Constructor
WoodFork.setName
MyBeanPostProcessor.postProcessBeforeInitialization for WoodFork
MyBeanPostProcessor.postProcessAfterInitialization for WoodFork
Student.Constructor
Student.setFork
MyBeanPostProcessor.postProcessBeforeInitialization for Student
Student.init
MyBeanPostProcessor.postProcessAfterInitialization for Student
WoodFork.forkMeat
Student.eatMeat
end
MyBeanPostProcessor.destory

转载地址:http://nglzl.baihongyu.com/

你可能感兴趣的文章
git中忽略UserInterfaceState.xcuserstate的方法
查看>>
【leetcode】307. Range Sum Query - Mutable
查看>>
《世界是数字的》读后感
查看>>
降维算法一览
查看>>
marquee知识点
查看>>
jQuery-DOM操作之创建、插入、删除元素
查看>>
自定义栈类型,具有找到站内最小元素的min函数 ,且min(),pop(),push()函数的时间复杂度为O(1)...
查看>>
redhat 5下源码安装nginx服务
查看>>
python优缺点分析及python种类,编码-课堂笔记及课后总结
查看>>
request和session作用域的意义
查看>>
大数据环境下互联网行业数据仓库/数据平台的架构之漫谈-续【转】
查看>>
SQLServer的最大连接数 超时时间已到 但是尚未从池中获取连接
查看>>
webpack学习笔记
查看>>
windows2008 RDP修改默认端口
查看>>
Nginx配置信息损毁又无备份时如何恢复
查看>>
libc.so.6: cannot open shared object file: No such file or diretory
查看>>
mysql oracle静默 一键安装脚本
查看>>
LINQ分页工具
查看>>
一张图让你明白IOS中bounds和frame的区别
查看>>
tex tab example
查看>>