Spring中的aware接口详情
作者:一滴水的坚持 时间:2023-11-29 10:48:29
Spring中有很多继承于aware
中的接口,这些接口到底是做什么用到的。
aware
,翻译过来是知道的,已感知的,意识到的,所以这些接口从字面意思应该是能感知到所有Aware
前面的含义。
先举个BeanNameAware
的例子,实现BeanNameAware
接口,可以让该Bean
感知到自身的BeanName
(对应Spring容器的BeanId
属性)属性,举个例子:
BeanNameAware接口的定义:
public interface BeanNameAware extends Aware {
void setBeanName(String name);
}
定义两个User
,一个实现BeanNameAware
,一个不实现。
package com.github.jettyrun.springinterface.demo.aware.beannameaware;
import org.springframework.beans.factory.BeanNameAware;
/**
* Created by jetty on 18/1/31.
*/
public class User implements BeanNameAware{
private String id;
private String name;
private String address;
public void setBeanName(String beanName) {
//ID保存BeanName的值
id=beanName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
package com.github.jettyrun.springinterface.demo.aware.beannameaware;
import org.springframework.beans.factory.BeanNameAware;
/**
* Created by jetty on 18/1/31.
*/
public class User2 {
private String id;
private String name;
private String address;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
在Spring配置文件中初始化两个对象:
<bean id="zhangsan" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User">
<property name="name" value="zhangsan"></property>
<property name="address" value="火星"></property>
</bean>
<bean id="lisi" class="com.github.jettyrun.springinterface.demo.aware.beannameaware.User2">
<property name="name" value="lisi"></property>
<property name="address" value="火星"></property>
</bean>
main方法测试一下
BeanNameAware
接口所起的作用。
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:application-beanaware.xml");
User user=context.getBean(User.class);
System.out.println(String.format("实现了BeanNameAware接口的信息BeanId=%s,所有信息=%s",user.getId(),user.toString()));
User2 user2=context.getBean(User2.class);
System.out.println(String.format("未实现BeanNameAware接口的信息BeanId=%s,所有信息=%s",user2.getId(),user2.toString()));
}
运行结果:
实现了BeanNameAware接口的信息BeanId=zhangsan,所有信息=User{id='zhangsan', name='zhangsan', address='火星'}
未实现BeanNameAware接口的信息BeanId=null,所有信息=User{id='null', name='lisi', address='火星'}
能够看到,我们在实现了BeanNameAware
的 User
中,获取到了Spring容器中的BeanId
(对应spring配置文件
中的id
属性),而没有实现BeanNameAware
的User2
,则不能获取到Spring容器中的Id属性。
所以BeanNameAware
接口是为了让自身Bean
能够感知到,获取到自身在Spring容器中的id属性。
同理,其他的Aware
接口也是为了能够感知到自身的一些属性。
比如实现了ApplicationContextAware
接口的类,能够获取到ApplicationContext
,实现了BeanFactoryAware
接口的类,能够获取到BeanFactory
对象。
来源:https://www.jianshu.com/p/c5c61c31080b
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis自动创建表和更新表结构
基于C#实现简易的键盘记录器
![](https://img.aspxhome.com/file/2023/1/88411_0s.png)
快速学习六大排序算法
![](https://img.aspxhome.com/file/2023/5/105175_0s.gif)
C#实现读取txt文件生成Word文档
![](https://img.aspxhome.com/file/2023/5/98215_0s.jpg)
springboot+jwt+springSecurity微信小程序授权登录问题
![](https://img.aspxhome.com/file/2023/7/71587_0s.png)
SpringBoot整合WebSocket的客户端和服务端的实现代码
Android实现渐变圆环、圆形进度条效果
![](https://img.aspxhome.com/file/2023/4/137254_0s.jpg)
Android 开发中layout下的子文件夹
![](https://img.aspxhome.com/file/2023/7/128957_0s.png)
安卓图片反复压缩后为什么普遍会变绿而不是其它颜色?
![](https://img.aspxhome.com/file/2023/0/138490_0s.png)
Java将Exception信息转为String字符串的方法
基于AForge实现C#摄像头视频录制功能
![](https://img.aspxhome.com/file/2023/6/89746_0s.png)
C#实现的上传图片、保存图片、加水印、生成缩略图功能示例
Android利用ViewPager实现带小圆球的图片滑动
![](https://img.aspxhome.com/file/2023/9/117089_0s.gif)
C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法
C#中==(双等于号)与equals()区别详解
![](https://img.aspxhome.com/file/2023/0/70440_0s.png)
基于Java中的数值和集合详解
Android应用内悬浮窗Activity的简单实现
![](https://img.aspxhome.com/file/2023/4/84704_0s.png)