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

诗人做的网站温州系统开发

诗人做的网站,温州系统开发,wordpress缓存插件汉化破解版,苏州网站开发网站开发费用1.Aware 接口的工作原理 Spring 提供了多个 XXXAware 接口(如 ApplicationEventPublisherAware、ApplicationContextAware、BeanFactoryAware 等),这些接口的核心作用是让 Bean 在初始化过程中自动获取特定的依赖。 实现 Aware 接口的 Bean…

1.Aware 接口的工作原理

Spring 提供了多个 XXXAware 接口(如 ApplicationEventPublisherAwareApplicationContextAwareBeanFactoryAware 等),这些接口的核心作用是让 Bean 在初始化过程中自动获取特定的依赖

  • 实现 Aware 接口的 Bean:当 Spring 容器创建一个实现了 Aware 接口的 Bean 时,容器会自动调用接口定义的 setXXX 方法,传入对应的依赖对象
  • 无需 @AutowiredAware 接口的注入由 Spring 容器在 Bean 初始化阶段自动触发,不需要显式使用 @Autowired 或构造函数注入。

2.Aware赋值过程演示

以ApplicationEventPublisherAware 的赋值过程为例,演示Aware赋值过程。

步骤 1:创建UserService的bean

当Spring 容器初始化 UserService(它实现了 ApplicationEventPublisherAware)时,检测到UserService 实现了 ApplicationEventPublisherAware 接口,先执行步骤2.

@Service
public class UserService implements ApplicationEventPublisherAware {private ApplicationEventPublisher publisher;@Overridepublic void setApplicationEventPublisher(ApplicationEventPublisher publisher) {this.publisher = publisher; // 这里会被 Spring 自动调用}// 其他方法...
}
步骤 2:Spring 容器检测到 Aware 接口

Spring容器检测到 UserService 实现了 ApplicationEventPublisherAware 接口,因此会:

  1. 查找 ApplicationEventPublisher 的实现 Bean:Spring 容器会自动将 ApplicationContext 作为 ApplicationEventPublisher 的实现(ApplicationContext 是 Spring 容器的核心接口,它直接实现了 ApplicationEventPublisher 接口。因此,Spring 容器会将 ApplicationContext 自身作为 ApplicationEventPublisher 的实现传入。)。
  2. 调用 setApplicationEventPublisher 方法:Spring容器自动调用setApplicationEventPublisher方法将 ApplicationContext 的实例赋值给 publisher 变量。
步骤 3:初始化完成,生成bean

最终,publisher 变量被赋值为 ApplicationContext 的实例,UserService初始化完成,生成对应的bean

总结:

当Spring容器创建UserService这个Bean的时候,它检查到这个Bean实现了Aware接口Spring 容器自动调用 setApplicationEventPublisher 方法,将 ApplicationContext 的实例注入到 publisher 变量中。

在这个过程中,publisher变量的赋值是通过Spring容器在Bean初始化阶段自动完成的,不需要开发者手动使用@Autowired或者其他注解。

3.Aware方式 vs @Autowired 

两种注入方式用法

使用Aware方式 和 使用@Autowired 引入bean的方式效果类似:

@Service
public class UserServiceWithAutowired {private final ApplicationEventPublisher publisher;@Autowiredpublic UserServiceWithAutowired(ApplicationEventPublisher publisher) {this.publisher = publisher;// 验证 publisher 是 ApplicationContext 的实例System.out.println("UserServiceWithAutowired.publisher class: " + publisher.getClass().getName()); //输出:UserServiceWithAutowired.publisher class: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext}//其他业务代码...
}
@Service
public class UserServiceWithAware implements ApplicationEventPublisherAware {private ApplicationEventPublisher publisher;/*** Aware 类会在Spring启动时自动设置 ApplicationEventPublisher** @param publisher*/@Overridepublic void setApplicationEventPublisher(ApplicationEventPublisher publisher) {this.publisher = publisher;System.out.println("UserServiceWithAware.publisher class: " + publisher.getClass().getName());// 输出:UserServiceWithAware.publisher class: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext}//其他业务代码...
}

启动应用后,上面两段代码中的publisher均能赋值完成,对应输出如下:

两种注入方式对比

方案 1(Aware 接口)的特点:
  • 无需注解:完全依赖接口实现,适合旧版 Spring 或某些特殊场景(如无法修改构造函数的遗留代码)。
  • 自动注入:Spring 容器在初始化 Bean 时自动调用 setXXX 方法。
  • 灵活性:可以注入多个 Aware 接口(例如同时实现 ApplicationContextAware 和 BeanFactoryAware)。
  • 注意事项:Aware接口的set方法必须严格按照命名规范,比如setApplicationEventPublisher,这样Spring才能正确识别并调用。如果方法名不对,Spring可能无法正确注入。
方案 2(直接注入 ApplicationEventPublisher)的优势:
  • 更简洁:通过 @Autowired 或构造函数注入,代码更清晰。
  • 符合现代 Spring 风格:推荐在新项目中使用,因为 Aware 接口在某些场景下可能显得冗余。

总结:Aware 接口适合需要与容器深度集成的场景,通过这种方式,Spring 实现了依赖注入的另一种形式,无需显式注解,完全依赖接口的约定。但现代开发更推荐直接通过依赖注入(如 @Autowired)的方式。


文章转载自:

http://XRA0F4Am.mtmph.cn
http://lOeNvFSF.mtmph.cn
http://7tEu9ZNz.mtmph.cn
http://t25dY3VS.mtmph.cn
http://xyqLVOiL.mtmph.cn
http://C6Vw57AD.mtmph.cn
http://84POpEte.mtmph.cn
http://1hv6lKj0.mtmph.cn
http://st0w9zS7.mtmph.cn
http://KBLWp9Kx.mtmph.cn
http://c2BopzuC.mtmph.cn
http://75YbT45s.mtmph.cn
http://yosmPAMU.mtmph.cn
http://C4EWvjfh.mtmph.cn
http://Ip6DptQ7.mtmph.cn
http://7fheLTSP.mtmph.cn
http://fIskcCQS.mtmph.cn
http://15p3mJxz.mtmph.cn
http://GFooW1r4.mtmph.cn
http://awF93DdW.mtmph.cn
http://EckSqGg9.mtmph.cn
http://0HERPDQD.mtmph.cn
http://Ai9NAiGU.mtmph.cn
http://ua59s6DB.mtmph.cn
http://7NWtu5wR.mtmph.cn
http://OstO9ytE.mtmph.cn
http://vcKLsU1y.mtmph.cn
http://3IJDTFTI.mtmph.cn
http://LyYz7cy4.mtmph.cn
http://O7juje3p.mtmph.cn
http://www.dtcms.com/wzjs/678523.html

相关文章:

  • 道滘镇做网站百度北京公司地址全部
  • ic电子网站建设招聘页面设计
  • 网站建设哪家好就推 鹏博资讯织梦网站地图制作教程
  • 个人博客网站建设预算做网站用什么软件编辑
  • 如何上传文件到网站400电话实名制认证网站
  • 网站的建设方法包括什么作用网站底色什么颜色好看
  • 用vs2012做简单网站佛山营销网站开发怎么选
  • 模版网站是什么意思网站开发安全机制
  • 培睿网站开发与设计一起做网店类型的网站
  • 还有多少用.net做网站的门户网下载
  • 小米手机网站建设总结北京互联网公司聚集地
  • 旅游景区网站源码济南做html5网站
  • 建立外贸网站多少钱专业的常州网站建设
  • 网站开发asp软件有哪些南京做网站南京乐识专业
  • 平江区建设局网站建设网站工作报告
  • 凌云县 城市建设 网站深圳网站建设clh
  • 国家重大建设项目库填报网站专业做汽车零部件平台的网站
  • php网站留言板模板下载山东网站定制设计公司
  • 网站怎么做快照怎么做网站后门
  • 福建建设工程信息网官网百度seo招聘
  • 单页网站开发实例下载wordpress rss 全文
  • 深圳专业做网站专业南京网站微信建设
  • 谷歌网站质量指南如何做网站栏目规划
  • 外贸网站建设注意什么主流软件开发工具
  • 定制化网站开发做的asp网站手机号码
  • 手机网站头部代码北京最新发布信息
  • 确山网站建设宁波网站优化找哪家
  • 什么浏览器可以看任何网站中小企业网站建设报告
  • 优秀企业网站建设wordpress侧边栏编辑
  • 静安网站建设公司大宗商品现货交易平台排名