目录
Spring Bean是Spring框架的核心概念,bean 的生命周期有 spring IOC容器管理,bean 生命周期包括:实例化 -> 属性注入 -> 处理Aware接口 -> BeanPostProcessor(初始化前) -> 自定义初始化方法 -> BeanPostProcessor (初始化后) -> 使用 -> 销毁
启动spring时,IOC容器会找到所有bean,通过反射实例化。
可通过字段加注解Autowired(字段注入,如果这个字段是接口并且已经有bean实现了,也会注入,属于接口注入),还可通过setter方法加注解Autowired(setter注入,setter的参数会自动注入)
如果bean实现了Aware下的接口(BeanNameAware
、BeanFactoryAware、
ApplicationContextAware
),Spring会自动调用相应的setter方法,将相关的资源注入进去。
Spring会检查实现了BeanPostProcessor接口的bean,调用postProcessBeforeInitialization
实现InitializingBean,定义方法。
Spring会检查实现了BeanPostProcessor接口的bean,调用postProcessAfterInitialization
可通过java注解PreDestroy,也可以实现DisposableBean
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- import org.springframework.stereotype.Component;
-
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
-
- //通过注解方式生命这是一个bean,交给springIOC容器管理
- @Component
- public class BeanDemo implements ApplicationContextAware, InitializingBean, DisposableBean {
-
- //字段注入
- @Autowired
- private ApplicationContext applicationContext1;
-
- private ApplicationContext applicationContext2;
-
- public BeanDemo() {
- System.out.println("1、通过反射创建bean");
- }
-
- //setter注入
- @Autowired
- public void setApplicationContext2(ApplicationContext applicationContext) {
- System.out.println("2、setter注入(Autowired方法)");
- this.applicationContext2 = applicationContext;
- }
-
- @Autowired
- private void init1() {
- System.out.println("2、Autowired方法");
- }
-
- @PostConstruct
- private void init2() {
- System.out.println("5、java注解PostConstruct一般用于执行一些初始化操作");
- }
-
- @Override
- public void afterPropertiesSet() {
- System.out.println("5、InitializingBean实现自定义初始化方法");
- }
-
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- System.out.println("3、aware接口处理, setApplicationContext方法");
- }
-
- @PreDestroy
- private void myPreDestroy() {
- System.out.println("7、java注解PreDestroy");
- }
-
- @Override
- public void destroy() {
- System.out.println("7、DisposableBean");
- }
- }
-
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.BeanPostProcessor;
- import org.springframework.context.ConfigurableApplicationContext;
- import org.springframework.stereotype.Component;
-
- @Component
- public class BeanManage implements BeanPostProcessor {
-
- @Autowired
- private ConfigurableApplicationContext context;
-
- @Override
- public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
- if (bean instanceof BeanDemo) {
- System.out.println("4、 postProcessBeforeInitialization" + beanName);
- }
- return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
- }
-
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
- if (bean instanceof BeanDemo) {
- System.out.println("6、 postProcessAfterInitialization" + beanName);
-
- context.close();
-
- }
- return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
- }
- }
控制台结果:
1、通过反射创建bean
2、Autowired方法
2、setter注入(Autowired方法)
3、aware接口处理, setApplicationContext方法
4、 postProcessBeforeInitializationbeanDemo
5、java注解PostConstruct一般用于执行一些初始化操作
5、InitializingBean实现自定义初始化方法
6、 postProcessAfterInitializationbeanDemo
7、java注解PreDestroy
7、DisposableBean