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

Spring内置功能

       Spring提供了一些内置功能,也可以在Bean的生命周期阶段提供一些额外功能。内置功能主要有Aware回调接口以及InitializingBean接口。

1.BeanNameAware

          实现该接口可以设置bean的名字,这是一个回调接口。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.beans.factory;public interface BeanNameAware extends Aware {void setBeanName(String var1);
}

2.ApplicationContextAware

     实现该接口可以设置ApplicationContext容器。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.context;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;public interface ApplicationContextAware extends Aware {void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

3.InitializingBean

       实现这个这个接口在Bean的初始化前可以做一些额外处理。

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//package org.springframework.beans.factory;public interface InitializingBean {void afterPropertiesSet() throws Exception;
}

4.测试类

package com.example.demo2.b04;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/11 20:42*/
public class Mybean implements BeanNameAware, ApplicationContextAware, InitializingBean {private static final Logger log = LoggerFactory.getLogger(b04Application.class);@Overridepublic void setBeanName(String name) {log.info("当前bean "+this+"名字叫:"+name);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {log.info("当前bean "+this+"容器是:"+applicationContext);}@Overridepublic void afterPropertiesSet() throws Exception {log.info("当前bean "+this+"初始化");}
}
package com.example.demo2.b04;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/11 20:41*/
public class b04Application {private static final Logger log = LoggerFactory.getLogger(b04Application.class);public static void main(String[] args) {GenericApplicationContext context = new GenericApplicationContext();context.registerBean("myBean", Mybean.class);context.refresh();context.close();}
}

结果:

            上面三个接口的方法都实现了,Aware接口先生效,后面才是InitializingBean接口。

5.对比@Autowired实现

       @Autowired也能实现上面的Spring的内置功能,下面举一个例子。

    @Autowiredpublic void test(ApplicationContext applicationContext){log.info("当前bean "+this+"容器是"+applicationContext);}

结果如下:

       由于没有注册Bean的后处理器所以没生效,添加解析@Autowired的后处理器。

package com.example.demo2.b04;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.context.support.GenericApplicationContext;/*** @author zhou* @version 1.0* @description TODO* @date 2025/10/11 20:41*/
public class b04Application {private static final Logger log = LoggerFactory.getLogger(b04Application.class);public static void main(String[] args) {GenericApplicationContext context = new GenericApplicationContext();context.registerBean("myBean", Mybean.class);context.registerBean(AutowiredAnnotationBeanPostProcessor.class);context.refresh();context.close();}
}

        相比于前面的内置功能,@Autowired的实现方式是一种拓展功能,它需要加对应的Bean后处理器才能生效,而且有时可能会失效,没有内置功能稳定。

http://www.dtcms.com/a/469420.html

相关文章:

  • Windows离线安装OpenSSH.Server
  • 【SELinux】解决 systemd 服务因权限问题无法启动(203/EXEC)的完整方案
  • maven下载与安装及在IDEA中配置maven
  • 云南省建设工程标准定额网站挣钱最快的游戏
  • git执行git remote关联了仓库后的.git文件夹目录下到底是些什么东西?优雅草卓伊凡
  • SpringBoot+Hutool+Vue实现导出
  • 中山建设厅网站首页山东济南网站推广
  • ⸢ 柒-Ⅲ⸥⤳ 可信纵深防御建设方案:数据使用可信端安全可信
  • PHP多维数组按指定字段排序usort自定义排序方法(或使用太空船操作符 <=> 进行比较,默认按升序排序)
  • Ubuntu 20.04 安装 Redis
  • etcd节点噶了导致的k8s集群瘫痪处理参考——筑梦之路
  • stm32底层项目20251011
  • http://localhost:7474/browser/ 登陆之后账号密码neo4j / neo4j 不对 页面出现以下:
  • 网站后台添加不了图片2008r2做网站
  • 学工网站建设博物馆网站建设说明
  • RabbitMQ概述,Rabbitmq是什么
  • C 语言12:字符串函数全解析
  • 国家城乡建设规划部网站邢台专业网站建设价格
  • 三层架构:解耦 JavaWeb 开发的核心范式
  • MySQL————表的约束
  • 速度达24.3MB/s,最新精简可用版!
  • 分业务采用差异化模式:全面提升 SQL Server 系统的并发性能、可靠性与数据准确性
  • 【Linux】应用层自定义协议与序列化
  • 文件上传漏洞: .htaccess文件
  • 【GD32】软件I2C
  • 温州产品推广网站服务网站建设方案
  • 08-docker综合应用
  • 电商网站建设与运营哦在线图片编辑助手
  • 十一款c++小游戏
  • 15-verilog的延时打拍问题记录