当前位置: 首页 > news >正文

网站建设地域名凡科送审平台官网

网站建设地域名,凡科送审平台官网,上海公司章程在哪里下载,怎么进wordpress1.生命周期 Spring应用中容器管理了我们每一个bean的生命周期,为了保证系统的可扩展性,同时为用户提供自定义的能力,Spring提供了大量的扩展点。完整的Spring生命周期如下图所示,绿色背景的节点是ApplictionContext生命周期特有的…

1.生命周期

Spring应用中容器管理了我们每一个bean的生命周期,为了保证系统的可扩展性,同时为用户提供自定义的能力,Spring提供了大量的扩展点。完整的Spring生命周期如下图所示,绿色背景的节点是ApplictionContext生命周期特有的节点,单纯从生命周期上来看,BeanFactory的生命周期和ApplicationContext的生命周期差异不大,不单独论述。

ApplicationContext和BeanFactory还有一个值得一提的差异是,ApplicationContext会自动查找容器内的BeanFactoryPostProcessor、InstantiationAwareBeanPostProcessor、BeanPostProcessor自动引用到上下文中;BeanFactory必须自己手动调用addBeanPostProcessor。

我把整个Bean的生命周期分为5个阶段,如图最左侧的大括号所示,分别是: 配置解析、实例化、依赖注入、初始化、销毁。下面我们会依次查看,它能做什么,Spring什么特性依赖于它,以及我们的开发中能怎么利用这个特性。

2. 配置解析

正常生命周期中,我们不直接参与配置解析,通过BeanFactoryPostProcessor的postProcessBeanFactory回调,我们能拿到ConfigurableListableBeanFactory,通过beanFactory.getBeanDefinition(beanName)对BeanDefinition对象做读取和设置。在Spring内部有两种使用场景

1. 做准备工作

在Bean实例化前,做准备工作。这类对象以CustomAutowireConfigurer、CustomEditorConfigurer为代表,CustomAutowireConfigurer继承BeanFactoryPostProcessor类,覆写postProcessBeanFactory,会从当前的BeanFactory中查找QualifierAnnotationAutowireCandidateResolver实例,并将customQualifierTypes里的注解类添加到QualifierAnnotationAutowireCandidateResolver中。

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {if (this.customQualifierTypes != null) {...DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory)beanFactory;...QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver)dlbf.getAutowireCandidateResolver();Iterator var4 = this.customQualifierTypes.iterator();while(var4.hasNext()) {...resolver.addQualifierType(customType);}}
}
2. 改Bean定义

在实例化前,修改BeanDefinition中的配置中,比如替换占位符。这类对象以PropertyPlaceholderConfigurer为代表,PropertyPlaceholderConfigurer间接继承了BeanFactoryPostProcessor,核心逻辑都在doProcessProperties中,这里的BeanDefinitionVisitor的作用就是使用valueResolver将BeanDefinition中的占位符替换为配置文件中的值。

protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) {BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver);...for(int var7 = 0; var7 < var6; ++var7) {...if (!curName.equals(this.beanName) || !beanFactoryToProcess.equals(this.beanFactory)) {BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName);...visitor.visitBeanDefinition(bd);}}...
}

看代码得到的额外收获是BeanDefinitionVisitor的使用,是典型的Visitor模式,当数据结构稳定,而操作不稳定时,用Visitor模式,能隔易变的逻辑独立在稳定的数据结构之外。

3. 实例化

3.1 postProcessBeforeInstantiation

这里将InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation做为实例化的一部分,因为postProcessBeforeInstantiation返回非null的情况下,将会跳过构造函数实例化,直接用postProcessBeforeInstantiation返回的对象做为bean实例。比如下面的例子,通过BeanFactory.getBean("globalProduct")返回的就是我们创建的id=5678的Product对象,并且跳过InstantiationAwareBeanPostProcessor的后两个回调函数。

public class ProductInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {if ("globalProduct".equals(beanName)) {System.out.println("postProcessBeforeInstantiation----->beanClass: " + beanClass.getName() + ",beanName: " + beanName);Product product = new Product();product.setId(5678L);product.setStaticScore(2.333F);product.setRelationScore(3.444F);product.setCategoryId(8765);return product;}return null;}...
}
3.2 构造函数

这里的构造函数实例化,不仅限于实际Java类的构造函数,还可以是FactoryBean.getObject,或者使用Spring的factory-bean、factory-method创建Bean的过程。

4. 依赖注入

4.1 InstantiationAwareBeanPostProcessor

通过InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation修改bean对象,完成属性注入。

@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {if ("globalProduct".equals(beanName)) {System.out.println("postProcessAfterInstantiation----->bean: " + bean + ",beanName: " + beanName);PropertyDescriptor prop = BeanUtils.getPropertyDescriptor(bean.getClass(),"id");Method writer = prop.getWriteMethod();writer.invoke(bean,Long.valueOf(2222)); // 将globaleProduct bean的id改为2222}return true;
}

此外通过postProcessProperties回调,直接修改现有的PropertyValues或者往PropertyValues里新增PropertyValue即可

@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {if ("globalProduct".equals(beanName)) {System.out.println("postProcessProperties----->pvs: " + pvs + ",bean: " + bean + ",beanName: " + beanName);MutablePropertyValues mpvs = (MutablePropertyValues) pvs;mpvs.addPropertyValue("id",3333); // 将globaleProduct bean的id改为3333return mpvs;}return pvs;
}
4.2 依赖注入

普通的setter方法注入、@Autowired基于方法入参、字段的注入。

4.3 回调接口

包括BeanNameAware、BeanFactoryAware、ApplicationContextAware,这3个接口都逻辑都很简单,提供一个set方法,容器将对于的对象通过set方法入参传递给bean实例。

private String beanName;
@Override
public void setBeanName(String name) {this.beanName = name;
}private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory = beanFactory;
}private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;
}

5.初始化

5.1 BeanPostProcessor

BeanPostProcessor有两个方法,postProcessBeforeInitialization在Bean调用InitializingBean、init-method等初始化方法前调用

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {if ("globalProduct".equals(beanName)) {System.out.println("postProcessBeforeInitialization>>> bean:" + bean);}return bean;
}

在Bean调用InitializingBean、init-method等初始化方法后调用

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {if ("globalProduct".equals(beanName)) {System.out.println("postProcessAfterInitialization>>> bean:" + bean);}return bean;
}
5.2 InitializingBean

需要Bean对象自己实现InitializingBean接口的afterPropertiesSet方法,这里我们只是简单的打印。

public class Product implements InitializingBean {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("Product.afterPropertiesSet");}
}
5.3 init-method、@PostConstruct

init-method和@PostConstruct作用和InitializingBean的afterPropertiesSet方法相同,用于做初始化动作。好处是我们的Bean不再需要继续一个Spring接口,而是通过配置或注解触发初始化方法调用。

通过配置init-method

@Bean(initMethod = "afterPropertiesSet")
public Product globalProduct() {Product product = new Product();...return product;
}

通过@PostConstruct注解

@PostConstruct
public void afterPropertiesSet() throws Exception {System.out.println("Product.afterPropertiesSet");
}

6. 销毁

6.1 DisposableBean

需要Bean对象实现DisposableBean的destroy方法,这里我们只是简单的打印

public class Product implements DisposableBean{public void destroy() throws Exception {System.out.println("Product.destroy");}
}
6.2 destroy-method、@PreDestroy

destroy-method和@PreDestroy的作用和DisposableBean的destroy方法作用一致,好处是不需要Bean实现Spring的接口。

通过destroy-method

@Bean(destroyMethod = "destroy")
public Product globalProduct() {Product product = new Product();...return product;
}

通过@PreDestroy

@PreDestroy
public void destroy() throws Exception {System.out.println("Product.destroy");
}

http://www.yayakq.cn/news/896354/

相关文章:

  • 迪庆企业网站建设各行业网站建设方案书
  • 花都手机网站建设seo是什么职务
  • 鞍山百度做网站杭州网站建设杭州沃迩夫
  • 东莞招聘网官方网站微信推广朋友圈广告
  • 网站浏览排名微信公众号开发步骤
  • 东台做网站哪家便宜无忧seo
  • 网站建设的风险识别注册网站应注意事项
  • 选服务好的网站建设公上海设计招聘网站
  • 承德建设局网站一站式建站价格
  • 北京 网站设计公司门户wordpress主题下载
  • 什么网站做微信公众账号网站设计开户
  • 企业型网站有哪些特点网站域名备案证书下载
  • 如何网站关键词优化设计公司网站建设
  • 做个网站怎样做的目前搜索引擎排名
  • 网站淘宝客怎么做成都 网站设计公司
  • 网站建设shopify帝国 cms 网站关键字
  • wordpress技术站主题电子商务网站建设产品
  • 个人工作室网站备案网站建设需要具备
  • 哪里有做网站开发前端做网站需要的技能
  • 哪些网站是用jsp做的怎么进行推广
  • 网站建设流程怎么样宜兴做网站多少钱
  • 网站想换个风格怎么做优化大师有必要花钱吗
  • 给公司做网站怎么样中国舆情监测公司排名
  • 专注赣州网站建设专业做数据的网站
  • 重庆建设网站公司上海网站建设不好
  • 网站开发服务费计入哪项费用wordpress如何改文章id
  • 营口门户网站建设百讯科技网站建设
  • 宝塔面板建站教程长沙官网优化技术
  • 奇月网络官方网站团购网站模板下载
  • 神兵网站建设火车票网站建设多少