Spring中常用重要的接口
Spring (ApplicationContext 初始化Bean的方法 refresh())
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.//创建Bean并post等方法
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
Spring 框架中想自己在bean的初始化阶段自定义一些逻辑,或者想获取一些资源,非常有用的接口有
BeanPostProcessor,// properties 设置完成,InitializingBean的 afterPropertiesSet(), init-method完成后对bean进行一些逻辑处理
InitializingBean,// 提供afterPropertiesSet(),位于init-method之前调用(这两方法都可用于实现bean init方法);
DestructionAwareBeanPostProcessor,//postProcessBeforeDestruction(Object bean, String beanName) 方法于 destory方法之前调用;
BeanFactoryAware // setBeanFactory(BeanFactory beanFactory) 接口使Bean初始化完成后获取BeanFactory资源,以便操作。类似的还有ApplicationContextAware....
public interface BeanPostProcessor {
/**property values已经设置完成,
* Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one; if
* <code>null</code>, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
/**
* Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
* initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding <code>bean instanceof FactoryBean</code> checks.
* <p>This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other BeanPostProcessor callbacks.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one; if
* <code>null</code>, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
/**已经完成了property 填充(完成InitializingBean 接口的afterPropertiesSet()调用,如果指定init-method 方法,也已经完成的调用(after..()之后))
* Initialize the given bean instance, applying factory callbacks
* as well as init methods and bean post processors.
* <p>Called from {@link #createBean} for traditionally defined beans,
* and from {@link #initializeBean} for existing bean instances.
* @param beanName the bean name in the factory (for debugging purposes)
* @param bean the new bean instance we may need to initialize
* @param mbd the bean definition that the bean was created with
* (can also be <code>null</code>, if given an existing bean instance)
* @return the initialized bean instance (potentially wrapped)
* @see BeanNameAware
* @see BeanClassLoaderAware
* @see BeanFactoryAware
* @see #applyBeanPostProcessorsBeforeInitialization
* @see #invokeInitMethods
* @see #applyBeanPostProcessorsAfterInitialization
*/
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
invokeAwareMethods(beanName, bean);
return null;
}
}, getAccessControlContext());
}
else {
invokeAwareMethods(beanName, bean);
} Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);//调用BeanPostProcessor的before方法
} try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
} if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);//调用
BeanPostProcessor的after方法
} return wrappedBean; }
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("spring.xml"));
bf 只是保存一些BeanDeinationMap, 并不会马上实例化,需要后续调用getBean才初始化:
ApplicationContext ac2 = new ClassPathXmlApplicationContext("spring.xml");
ac2 会马上将spring.xml中定义的bean实例化加到factory的缓存中。
例子用ApplicationContext :
spring.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<alias name="user" alias="testxx"/>
<bean id="father" abstract="true">
<property name="local" value="Us"/>
</bean>
<bean id="user" class="com.donghua.bean.User" parent="father">
<property name="name" value="Allen"/>
<property name="age" value="6"/>
</bean>
<bean id="user3" class="com.donghua.bean.User3" init-method="myInit" destroy-method="destroy">
<property name="name" value="Allen3"/>
<property name="age" value="61"/>
</bean>
<bean id="factory" class="com.donghua.bean.MyBeanFactory"/>
</beans>
User3:
package com.donghua.bean; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; public class User3 implements BeanPostProcessor, InitializingBean, DestructionAwareBeanPostProcessor,BeanFactoryAware
{
BeanFactory beanFactory;
private String name;
private int age; public String getName()
{
return name;
} public void setName(String name)
{
this.name = name;
} public int getAge()
{
return age;
} public void setAge(int age)
{
this.age = age;
} public void myInit()
{
System.out.println("this is my init....."+this.getAge());
} @Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
} @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("This is the post before..............");
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("This is the post after..............");
return bean;
} @Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("this is InitializingBean method..."
+ "(called by before init method, after set property values)."+this.getAge());
} @Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("this is DestructionAwareBeanPostProcessor, called when destroy bean...");
} public void destroy()
{
System.out.println("this is destroy method....");
} @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
// TODO Auto-generated method stub
this.beanFactory = beanFactory;
} public void getUser2()
{
User u =(User) beanFactory.getBean("user");
System.out.println(u.toString()+" this is user2...");
}
}
package com.donghua.test; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; import com.donghua.anotation.User2;
import com.donghua.bean.MyBeanFactory;
import com.donghua.bean.User;
import com.donghua.bean.User3; public class Test { public static void main(String[] args)
{
ApplicationContext ac2 = new ClassPathXmlApplicationContext("spring.xml");
User3 u3 = (User3) ac2.getBean("user3");
System.out.println(u3);
u3.getUser2();
((ClassPathXmlApplicationContext) ac2).close(); } }
输出顺序:
this is InitializingBean method...(called by before init method, after set property values).61 //InitializingBean
this is my init.....61 //init-method
This is the post before.............. //BeanPostProcesser
This is the post after..............
This is the post before..............
This is the post after..............
User [name=Allen3, age=61]
User [name=Allen, age=6] this is user2...
this is DestructionAwareBeanPostProcessor, called when destroy bean... // DestructionAwareBeanPostProcessor
this is DestructionAwareBeanPostProcessor, called when destroy bean...
this is destroy method.... //destory method
Spring中常用重要的接口的更多相关文章
- Spring中常用的23中设计模式
1.spring 中常用的设计模式有23中 分类 设计模式 创建型 工厂方法模式(FactoryMethod).抽象工厂模式(AbstractFactory).建造者模式(Builder).原型 ...
- 简单了解Spring中常用工具类_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...
- spring 中常用的配置项
1.spring 中常用的配置项 application.properties #端口 server.port=8081 #调试模式 debug=false #上下文 #一般情况下,小项目通常都是在t ...
- getHibernateTemplate()(Spring中常用的hql查询方法)
Spring中常用的hql查询方法(getHibernateTemplate()) --------------------------------- 一.find(String queryStrin ...
- spring中常用工具类介绍
http://www.cnblogs.com/langtianya/p/3875103.html 文件资源操作 Spring 定义了一个 org.springframework.core.io ...
- Spring 中常用注解原理剖析
前言 Spring 框架核心组件之一是 IOC,IOC 则管理 Bean 的创建和 Bean 之间的依赖注入,对于 Bean 的创建可以通过在 XML 里面使用 <bean/> 标签来配置 ...
- Spring中常用的注解,你知道几个呢?
今天给大家分享下Spring中一般常用的注解都有哪些.可能很多人做了很长是了但有些还是不知道一些注解,不过没有关系,你接着往下看. Spring部分 1.声明bean的注解 @Component 组件 ...
- 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题
1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...
- 0000 - Spring 中常用注解原理剖析
1.概述 Spring 框架核心组件之一是 IOC,IOC 则管理 Bean 的创建和 Bean 之间的依赖注入,对于 Bean 的创建可以通过在 XML 里面使用 <bean/> 标签来 ...
随机推荐
- python -- 模块与类库
一.模块 模块(Module)是由一组类.函数和变量组成的,模块文件的扩展名是.py或.pyc 在使用模块之前,需要先使用import语句导入这个模块. 语法格式如下: import 模块名 from ...
- JAVA基础(代码)练习题61~90
JAVA基础 61.设计一个方法打印数组{'a','r','g','s','e','r'}中下标为1和3的的元素 package Homework_90; /** * 设计一个方法打印数组{'a',' ...
- mysql免安装版下载及安装教程
第一步:下载 下载地址:http://dev.mysql.com/downloads/mysql/ 点击图中红色箭头Archives,可以下载自己想要的mysql版本,如图: 下载后解压,放在自己想要 ...
- 【洛谷P1140 相似基因】动态规划
分析 f[i][j] 表示 1数组的第i位和2数组的第j位匹配的最大值 f[1][1]=-2 f[2][1]=-2+5=3 f[3][1]=-2+5+5=8 三个决策: 1.由f[i-1][j-1]直 ...
- 大数据学习(06)——Ozone介绍
前面几篇文章把Hadoop常用的模块都学习了,剩下一个新模块Ozone,截止到今天最新版本是0.5.0Beta,还没出正式版.好在官方网站有文档,还是中文版的,但是中文版资料没有翻译完整,我试着把它都 ...
- 比POSTMAN更好用!在国产接口调试工具APIPOST中使用Mock
APIPOST可以让你在没有后端程序的情况下能真实地返回接口数据,你可以用APIPOST实现项目初期纯前端的效果演示,也可以用APIPOST实现开发中的数据模拟从而实现前后端分离.在使用APIPOST ...
- python aes pkcs7加密
# -*- coding: UTF-8 -*- from Crypto.Util.Padding import pad from Crypto.Cipher import AES import bas ...
- springboot 中 inputStream 神秘消失之谜
序言 最近小明接手了前同事的代码,意料之外.情理之中的遇到了坑. 为了避免掉入同一个坑两次,小明决定把这个坑记下来,并在坑前立一个大牌子,避免其他小伙伴掉进去. HTTPClient 模拟调用 为了把 ...
- 那些 Unix 命令替代品们「GitHub 热点速览 v.21.32」
作者:HelloGitHub-小鱼干 好用的 Unix 命令替代工具能让你事半功倍,例如,bat 便是个带着高亮特性的加强版 cat,就像你用了 oh my zsh 之后便会感受到它的强大.同样好用的 ...
- WPS函数
vlookup函数:=VLOOKUP(lookup_value,table_array,col_index_num,range_lookup) 官方解释:其逻辑为在某一区间内搜索区间外某一单元格的值, ...