可以看到,Aware接口是Spring 3.1版本中引入的接口,在Aware接口中,并未定义任何方法。
接下来,我们看看都有哪些接口继承了Aware接口,如下所示。
![](https://upload-images.jianshu.io/upload_images/24195226-208cd59e6a23affc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
## XxxAware接口案例
接下来,我们就挑选几个常用的XxxAware接口来进行简单的说明。
ApplicationContextAware接口使用的比较多,我们先来说说这个接口,通过ApplicationContextAware接口我们可以获取到IOC容器。
首先,我们创建一个Blue类,并实现ApplicationContextAware接口,在实现的setApplicationContext()中将ApplicationContext输出,如下所示。
package io.mykit.spring.plugins.register.bean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
- @author binghe
- @version 1.0.0
- @description 测试ApplicationContextAware接口
*/
public class Blue implements ApplicationContextAware {
private ApplicationContext applicationContext
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(“传入的ioc:” + applicationContext);
this.applicationContext = applicationContext;
}
}
我们也可以为Blue类同时实现几个XxxAware接口,例如,使Blue类再实现一个BeanNameAware接口,我们可以通过BeanNameAware接口获取到当前bean在Spring容器中的名称,如下所示。
package io.mykit.spring.plugins.register.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试ApplicationContextAware接口
*/
public class Blue implements ApplicationContextAware, BeanNameAware {
private ApplicationContext applicationContext
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(“传入的ioc:” + applicationContext);
this.applicationContext = applicationContext;
}@Override
public void setBeanName(String name) {
System.out.println(“当前bean的名字”);
}
}
接下来,我们再实现一个EmbeddedValueResolverAware接口,我们通过EmbeddedValueResolverAware接口能够获取到StringValue解析器。如下所示。
package io.mykit.spring.plugins.register.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.StringValueResolver;
/**
-
@author binghe
-
@version 1.0.0
-
@description 测试ApplicationContextAware接口
*/
public class Blue implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(“传入的ioc:” + applicationContext);
this.applicationContext = applicationContext;
}@Override
public void setBeanName(String name) {
System.out.println(“当前bean的名字”);
}@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
String resolveStringValue = resolver.resolveStringValue(“你好${os.name} 年龄:#{20*18}”);
System.out.println(“解析后的字符串为:” + resolveStringValue);
}
}
接下来,我们需要在Blue类上标注@Component注解将Blue类添加到IOC容器中,如下所示。
@Component
public class Blue implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
运行AutowiredTest类的testAutowired02()方法,输出的结果信息如下所示。
postProcessBeforeInitialization…autowiredConfig=>io.mykit.spring.plugins.register.config.AutowiredConfig
E
n
h
a
n
c
e
r
B
y
S
p
r
i
n
g
C
G
L
I
B
EnhancerBySpringCGLIB
EnhancerBySpringCGLIBd3c83622@1c93084c
postProcessAfterInitialization…autowiredConfig=>io.mykit.spring.plugins.register.config.AutowiredConfig
E
n
h
a
n
c
e
r
B
y
S
p
r
i
n
g
C
G
L
I
B
EnhancerBySpringCGLIB
EnhancerBySpringCGLIBd3c83622@1c93084c
postProcessBeforeInitialization…personDao=>PersonDao{remark=‘1’}
postProcessAfterInitialization…personDao=>PersonDao{remark=‘1’}
postProcessBeforeInitialization…personDao2=>PersonDao{remark=‘2’}
postProcessAfterInitialization…personDao2=>PersonDao{remark=‘2’}
postProcessBeforeInitialization…personService=>PersonService{personDao=PersonDao{remark=‘2’}}
postProcessAfterInitialization…personService=>PersonService{personDao=PersonDao{remark=‘2’}}
postProcessBeforeInitialization…personController=>io.mykit.spring.plugins.register.controller.PersonController@48ae9b55
postProcessAfterInitialization…personController=>io.mykit.spring.plugins.register.controller.PersonController@48ae9b55
执行了Animal类的无参数构造方法
postProcessBeforeInitialization…animal=>io.mykit.spring.plugins.register.bean.Animal@c267ef4
执行了Animal类的初始化方法。。。。。
postProcessAfterInitialization…animal=>io.mykit.spring.plugins.register.bean.Animal@c267ef4
当前bean的名字:blue
解析后的字符串为:你好Windows 10 年龄:360
传入的ioc:org.springframework.context.annotation.AnnotationConfigApplicationContext@5ecddf8f, started on Wed Aug 19 00:10:13 CST 2020
postProcessBeforeInitialization…blue=>io.mykit.spring.plugins.register.bean.Blue@55182842
postProcessAfterInitialization…blue=>io.mykit.spring.plugins.register.bean.Blue@55182842
Cat类的构造方法…
postProcessBeforeInitialization…cat=>io.mykit.spring.plugins.register.bean.Cat@76505305
Cat的postConstruct()方法…
postProcessAfterInitialization…cat=>io.mykit.spring.plugins.register.bean.Cat@76505305
调用了Dog的有参构造方法
postProcessBeforeInitialization…dog=>Dog{cat=io.mykit.spring.plugins.register.bean.Cat@76505305}
postProcessAfterInitialization…dog=>Dog{cat=io.mykit.spring.plugins.register.bean.Cat@76505305}
postProcessBeforeInitialization…employee=>io.mykit.spring.plugins.register.bean.Employee@74235045
postProcessAfterInitialization…employee=>io.mykit.spring.plugins.register.bean.Employee@74235045
postProcessBeforeInitialization…fish=>Fish{cat=io.mykit.spring.plugins.register.bean.Cat@76505305}
postProcessAfterInitialization…fish=>Fish{cat=io.mykit.spring.plugins.register.bean.Cat@76505305}
Fish{cat=io.mykit.spring.plugins.register.bean.Cat@76505305}
Cat的preDestroy()方法…
执行了Animal类的销毁方法。。。。。
1200页Java架构面试专题及答案
小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞
CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】
百度、字节、美团等大厂常见面试题
讲解视频】](https://codechina.csdn.net/m0_60958482/java-p7)**
[外链图片转存中…(img-99sLiy46-1630575607558)]
[外链图片转存中…(img-5ghQ38DR-1630575607560)]