Spring初始化Bean的过程

最近打算写一个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="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd " >

<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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值