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. 关于int指令

    1.关于int指令 格式:int n     n为中断类型码: 作用:     调用n号中断程序:   指令“int n”的执行过程:     1]获取中断类型码n     2]标志寄存器入栈,IF. ...

  2. 用jstl的if或when标签判断字符串是否为空

    在jsp页面用到jstl的if或when标签判断字符串不为空的时候,书写格式: <c:when test="${not empty paramName}"> </ ...

  3. 创建springboot项目的三种方法(参考

    https://blog.csdn.net/mousede/article/details/81285693 https://blog.csdn.net/weixin_42194143/article ...

  4. codeforces271D

    Good Substrings CodeForces - 271D 给你一个只包含小写字母的字符串s.问你在这个字符串中有多少个不同的子串.且要求这些子串中不得出现超过k个的特殊字母.*子串s1和子串 ...

  5. vue-cli 4.0.5 配置环境变量样例

    在项目根目录下建 .env 文件,环境变量无论运行何种模式均可获取其设置值. vue 中主要有三种模式: development.test.production,在 package.json 中目前三 ...

  6. C++回调函数、静态函数、成员函数踩过的坑。

    C++回调函数.静态函数.成员函数踩过的坑. 明确一点即回调函数需要是静态函数.原因: 普通的C++成员函数都隐含了一个this指针作为参数,这样使得回调函数的参数和成员函数参数个数不匹配. 若不想使 ...

  7. AWS EC2 PV Drivers 驱动升级

    问题 从2019-10-23起,我的AWS实例不断的重启(大概6个小时左右),或者连接不上(远程连接不上并PING不通IP),但控制台显示running. 分析与解决方法 通过查看dump文件,发现是 ...

  8. 深入聚焦 call,apply 和 bind

    在JavaScript 中,call.apply 和 bind 是 Function 对象自带的三个方法,这三个方法的主要作用是改变函数中的 this 指向,从而可以达到`接花移木`的效果.本文将对这 ...

  9. phpmyadmin 导入sql报错(sql为phpstudy内置数据库导出来)

    解决方法 1.打开sql,把头部注释去掉

  10. JDBC——JDBC基础

    1.JDBC与数据库的交互过程概括性来说,JDBC与数据库交互有以下这些步骤:1.建立一个到数据库的连接.2.在数据库中对表执行检索.创建,或修改的SQL查询.3.关闭到数据库的连接.JDBC的类和接 ...