Spring IoC 学习(3)
来源:互联网 发布:软件本地数据库查找 编辑:程序博客网 时间:2024/06/05 08:55
原文地址
前言
前面因为总结的累了,把IoC的两个步骤,只写了一半,就仅仅把容器启动的方面说了说,对于实例化的阶段,我前面并没有说,在这节中,准备讲一讲,实例化阶段。
生命周期
基础生命周期简图
这个部分,其实实例化,一般都是用反射或者cglib,底层封装的也比较深,我随着代码debug的过程中,也没有接触到这个部分。但是在实例化bean的过程中,还是看到了挺多东西。
生命周期的图,基本上有可能是以下这种
从图中可以看到,在这个阶段,最重要的不是实例化本身,而是实例化前后会做的一些操作。实例化有些不同的,应该就是在实例化时可能会遇到绑定属性的相关操作,这个时候不是用传统的反射来做,而是用BeanWrapper来包装绑定。有个印象即可。
BeanFactory与ApplicationContext生命周期简图
BeanFactory
ApplicationContext
以上两图为借用
各种拓展接口
各色的Aware接口
当对象实例化完成并且相关属性以及依赖设置完成之后,Spring容器会检查当前对象实例是否实现了一系列的以Aware命名结尾的接口定义。如果是,则将这些Aware接口定义中规定的依赖注入给当前对象实例。
下面总结一下各种Aware接口以及作用
BeanPostProcessor
我们看一下这个接口
package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;/** * */public interface BeanPostProcessor { /** * 初始化之前做操作 */ Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; /** * 初始化之后做操作 */ Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;}
BeanPostProcessor的应用场景
① 在注解时使用
在使用Spring构建目的时候,现在应该很多人都习惯于用注解了,因为注解简单。@Component @Controller @Service @Repository @Autowired 等注解来便捷开发,下面来探讨BeanPostProcessor在@Autowired 中的运用。
在使用@Autowired之前需要在容器中配置AutowiredAnnotationBeanPostProcessor。
② 处理Aware接口类
我们可以来看一小段ApplicationContextAwareProcessor的代码
package org.springframework.context.support;/** * 可以看到是实现自BeanPostProcessor的 */class ApplicationContextAwareProcessor implements BeanPostProcessor { /** * 略去部分代码 */ /** * 在初始化之前做的操作 */ @Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { AccessControlContext acc = null; if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { invokeAwareInterfaces(bean); return null; } }, acc); } else { // 直奔重点,invoke这些Aware接口 invokeAwareInterfaces(bean); } return bean; } private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver( new EmbeddedValueResolver(this.applicationContext.getBeanFactory())); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } } /** * 初始化之后就没有做其他操作了 */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) { return bean; }}
自定义BeanPostProcessor
当然,每个人都可以自己写一个BeanPostProcessor的实现类。
不过写完之后注意要在Spring配置文件中配置一下。具体操作:
http://blog.csdn.net/caihaijiang/article/details/35552859
InitializingBean、init-method和@PostConstruct
这个两个东西,其实都是做一件事,就是在bean的初始化阶段做一些其他的操作。
比如,在有些情况下,某个业务对象实例化完成后,还不能处于可以使用状态。这个时候就可以让该业务对象实现该接口,并在方法afterPropertiesSet()中完成对该业务对象的后续处理。
以上这段文字是摘抄下来的,但是我真的想不到,到底为什么要这样操作,你说要改变初始化的状态,那在一开始初始化时直接改成那个状态不就可以了吗?为什么要在这里做变化?不懂。但是操作就是,在bean初始化阶段做操作。
这种操作,有三种方式来做InitializingBean、init-method和@PostConstruct。
InitializingBean
这是一个接口,只有一个方法。
public interface InitializingBean { void afterPropertiesSet() throws Exception;}
如果一个bean想要在初始化阶段做操作,第一种方法就是实现这个接口
public Person implements InitializingBean { void afterPropertiesSet() throws Exception{ System.out.println(" 初始化阶段操作 ") }}
但是这种方式,其实还是会有点儿问题,这个对象和Spring的耦合度比较高。如果想使这个耦合度比较低,那么就用其他的两种方法了。
init-method
用一个例子,就能很好的把这个东西说清楚。
Person
class Person{ ... void eat(){ System.out.println("I am eating..."); }}
beans.xml
<beans> <bean id="person" class="Person" . init-method="eat"></bean>...</beans>
到时候实例化Person的时候,就会调用这个eat方法了。
@PostConstruct
其实这个注解和init-method是一样的。
person
class Person{ ... @PostContruct void eat(){ System.out.println("I am eating..."); }}
DisposableBean、destroy-method和@PreDestroy
在Bean销毁之前肯定也可以做些操作,这三者的特点和用法,其实都和初始化那部分差不多。不同的地方在下面这部分代码处体现。
Person
class Person{ ... @PostContruct void eat(){ System.out.println("I am eating..."); } @PreDestroy void sleep(){ System.out.println("I will go to sleep..."); }}
Main
class Main{ public static void main(String [] args){ ApplicationContext ac=new ClasspathXmlApplicationContext("beans.xml"); Person person = (Person) ac.getBean("person"); // 不一样之处,销毁时要调用,不然没人知道你什么不要。 person.sleep(); }
beans.xml
<beans> <bean id="person" class="Person" > ... </bean></beans>
总结
原文地址
- Spring IoC 学习(3)
- Spring IoC 学习(3)
- spring学习3-IoC概述
- 技术文章 | Spring IoC 学习(3)
- spring IoC学习 ------IoC基本概念
- Spring学习笔记:3-IoC中的国际化
- Spring学习3:IOC容器概述
- Spring IoC学习笔记
- Spring学习-------IOC
- Spring Ioc学习(一)
- Spring Ioc学习(二)
- spring ioc学习
- Spring IOC 源码学习
- Spring学习-IOC容器
- Spring 学习之 IOC
- spring Ioc 的学习
- spring学习之Ioc
- Spring学习之IOC
- Android开发笔记(一百四十九)约束布局ConstraintLayout
- Android Service学习笔记--Service知识概要
- shell 编程实例学习
- JAVA面试,最起码,你应该知道这些
- Matlab符号计算求导与化简
- Spring IoC 学习(3)
- 网易2018笔试题三
- idea搭建ssm框架
- 学生信息管理系统
- LeetCode 520. Detect Capital 自己的解法
- VS2010调试-显示堆栈窗口
- codeforces 842C dp
- 错误: 找不到或无法加载主类 myeclipse maven
- Codeforces Round #430 (Div. 2) D. Vitya and Strange Lesson(01Trie)