1.Aware接口

Awear 这个单词的意思是知道的,所以可以猜想以Awear 结尾的接口意思可以把他知道的东西告诉我们。常用的Awear接口有 ApplicationContextAware和 BeanFactoryAware,另外还有BeanNameAware 和EnvironmentAware

(1)ApplicationContextAware 接口 可以返回  ApplicationContext,也就是容器本身,我们可以通过ApplicationContext 这个容器对象做很多事,最常见的是获取Bean,和判断当前容器是否包含某个组件。下面的示例是通过ApplicationContext 获取Bean的示例(工程基于SpringBoot2.2.1.RELEASE)

package com.spring.hook.compent;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 13:51
* @desc
*/
@Component
public class SpringContextAware implements ApplicationContextAware { private static ApplicationContext app;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
app = applicationContext;
} public static<T> T getBeanByClass(Class<T> clazz){
return app.getBean(clazz);
} public static Class<?> getBeanByType(String type){
return app.getType(type);
} @SuppressWarnings("unchecked")
public static <T> T getBeanByName(String name){
return (T) app.getBean(name);
}
}

(2)BeanFactoryAware 。通过实现 BeanFactoryAware 可以获取BeanFactory。Beanfactory 作用和ApplicationContext 类似,BeanFactory 比ApplicationContext更底层一些, 当然功能更加单一一些,ApplicationContext 在此基础做了一些加强,比如多了国际化等一些信息。

package com.spring.hook.compent;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 16:48
* @desc
*/
@Component
public class BeanFactoryContext implements BeanFactoryAware{ private static BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
BeanFactoryContext.beanFactory = beanFactory;
} public static<T> T getBeanByClass(Class<T> clazz){
return beanFactory.getBean(clazz);
}
}

(3)BeanNameAware。这个接口个人感觉作用不明显,通过实现这个接口可以获取当前Bean的名称。

package com.spring.hook.compent;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 14:19
* @desc
*/
@Component
public class Car implements BeanNameAware{ @Override
public void setBeanName(String s) {
System.out.println("BeanName:"+s);
} private String brand; private String color; public String getCarAttr(){
return "品牌:"+brand+" 颜色:"+color;
} public void setBrand(String brand) {
this.brand = brand;
} public void setColor(String color) {
this.color = color;
}
}

(4)EnvironmentAware 通过实现这个接口可以获取当前运行环境的一些信息。

package com.spring.hook.compent;

import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 17:25
* @desc
*/
@Component
public class EnvironmentContext implements EnvironmentAware {
@Override
public void setEnvironment(Environment environment) {
String[] activeProfile = environment.getActiveProfiles();
String[] defaultProfile = environment.getDefaultProfiles();
String port = environment.getProperty("dog.type");
}
}

2.InitializingBean,DisposableBean 通过实现这两个接口可以在Bean初始化后和摧毁钱做一些事。InitializingBean 接口的afterPropertiesSet() 方法在set属性完成后执行,DisposableBean 的 destroy() 方法在bean摧毁前BeanFactory执行

package com.spring.hook.compent;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 17:45
* @desc
*/
@Component
public class Fruit implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
System.out.println("我要被销毁了");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("我原来的名字叫:"+name);
name = "苹果";
} private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

3.PostProcessor 这个接口被称为后置处理器,常见的有:BeanPostProcessor,BeanFactoryPostProcessor ,分别为Bean的后置处理器和BeanFactory的后置处理器。

(1)BeanPostProcessor 能再Bean的初始化前后做一些事,但是个人感觉很功能很单一,无法返回具体的Bean

package com.spring.hook.compent;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 18:19
* @desc
*/
@Component
public class BeanPostProcessorImpl implements BeanPostProcessor{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化前BeanName:"+beanName);
return bean;
} @Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("初始化后BeanName:"+beanName);
return bean;
} }

(2)BeanFactoryPostProcessor

package com.spring.hook.compent;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component; /**
* @Author lizhilong
* @create 2019/11/28 18:29
* @desc
*/
@Component
public class BeanPostFactoryContextImpl implements BeanFactoryPostProcessor { private static ConfigurableListableBeanFactory beanFactory; @Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
beanFactory = configurableListableBeanFactory;
if(beanFactory.containsBean("fruit")){
BeanDefinition fruit = beanFactory.getBeanDefinition("fruit");
MutablePropertyValues propertyValues = fruit.getPropertyValues();
propertyValues.add("name","香蕉"); }
Fruit fruit = (Fruit)beanFactory.getBean("fruit");
System.out.println("我现在的名字叫:"+fruit.getName());
}
}

示例中我们在不同的时刻为 Fruit 的name属性赋值,运行结果:

我原来的名字叫:香蕉
我现在的名字叫:苹果

Spring 中的几个常用的钩子接口的更多相关文章

  1. Spring中Bean管理的常用注解

    在Spring中,主要用于管理bean的注解分为四大类:1.用于创建对象.2.用于给对象的属性注入值.3.用于改变作用的范围.4.用于定义生命周期.这几个在开发中经常接触到,也可以说每天都会遇见.其中 ...

  2. spring中实现基于注解实现动态的接口限流防刷

    本文将介绍在spring项目中自定义注解,借助redis实现接口的限流 自定义注解类 import java.lang.annotation.ElementType; import java.lang ...

  3. Spring中@Import的各种用法以及ImportAware接口

    @Import 注解 @Import注解提供了和XML中<import/>元素等价的功能,实现导入的一个或多个配置类.@Import即可以在类上使用,也可以作为元注解使用. @Target ...

  4. 转:Spring中事物管理

    1.什么是事务? 事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败 2.事物具有四大特性ACID 说到事务,就不得不说其4大特性,主要如下 原子性:(atomicity) 原子性指的是事务是 ...

  5. Spring中的BeanFactory与FactoryBean看这一篇就够了

    前言 理解FactoryBean是非常非常有必要的,因为在Spring中FactoryBean最为典型的一个应用就是用来创建AOP的代理对象,不仅如此,而且对理解Mybatis核心源码也非常有帮助!如 ...

  6. JavaEE开发之Spring中的条件注解组合注解与元注解

    上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...

  7. JavaEE开发之Spring中的条件注解、组合注解与元注解

    上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...

  8. spring中的BeanDefinitionRegistryPostProcessor

    spring中的BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口,BeanFactoryPostProcessor的作用 ...

  9. Spring 中读取文件-ResourceLoaderAware

    Spring 中读取文件-ResourceLoaderAware 概述 Spring ResourceLoader为我们提供了一个统一的getResource()方法来通过资源路径检索外部资源.从而将 ...

随机推荐

  1. 提示 ToolTip

    <StackPanel> <Button Content="按钮1" ToolTip="这是个按钮1" HorizontalAlignment ...

  2. PHP基础教程-APACHE

    兄弟连:如何配置APACHE.首先,安装并配置PHP3 1.解开压缩包到你喜欢的目录如:C:PHP3 2.把C:php3php3.ini-inst文件改名成PHP3.INI并拷贝到C:windows ...

  3. TensorFlow使用记录 (五): 激活函数和初始化方式

    In general ELU > leaky ReLU(and its variants) > ReLU > tanh > logistic. If you care a lo ...

  4. vscode编辑器

    插件 Auto Close Tag   自动关闭标签 Auto Rename Tag 自动修改标签 Bracket Pair Colorizer  多层括号不同颜色显示 EditorConfig fo ...

  5. 用Python操作excel文档

    使用Python第三方库 这一节我们学习如何使用Python去操作Excel文档.如果大家有人不知道Excel的话,那么建议先学一学office办公基础.这里想要操作Excel,必须安装一个Pytho ...

  6. Latex里引用多个公式,如何将公式合并?

    如果是想要的效果:(1)-(3),怎么操作?类似于用\cite引用多个文献那样吗? 1. \eqref{lable 1, lable 2, label 3}? 得到的结果:3个问号 ??? 2.\eq ...

  7. Git本地安装

    1 Git简介 Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码 ...

  8. idea如果发生@override is not allowed when implement 错误,可以在Project Structure-Modules中更改Language level,设置为6以上的。

  9. Ngrinder脚本开发各细节锦集(groovy)

    Ngrinder脚本开发各细节锦集(groovy) 1.生成随机字符串(import org.apache.commons.lang.RandomStringUtils) 数字:RandomStrin ...

  10. mvp解读

    mvp存在的问题 1.业务复杂时,可能使得Activity变成更加复杂,比如要实现N个IView,然后写更多个模版方法. 2.业务复杂时,各个角色之间通信会变得很冗长和复杂,回调链过长. 3.Pres ...