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容器。

java 复制代码
//
// 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的初始化前可以做一些额外处理。

java 复制代码
//
// 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.测试类

java 复制代码
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);

    @Override
    public void setBeanName(String name) {
        log.info("当前bean "+this+"名字叫:"+name);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("当前bean "+this+"容器是:"+applicationContext);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("当前bean "+this+"初始化");
    }
}
java 复制代码
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的内置功能,下面举一个例子。

java 复制代码
    @Autowired
    public void test(ApplicationContext applicationContext){
        log.info("当前bean "+this+"容器是"+applicationContext);
    }

结果如下:

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

java 复制代码
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后处理器才能生效,而且有时可能会失效,没有内置功能稳定。

相关推荐
铉铉这波能秀2 小时前
如何在Android Studio中使用Gemini进行AI Coding
android·java·人工智能·ai·kotlin·app·android studio
_Yoke2 小时前
Java 枚举多态在系统中的实战演进:从枚举策略到自动注册
java·springboot·策略模式
SHUIPING_YANG2 小时前
完美迁移:将 nvm 和 npm 完全安装到 Windows D 盘
前端·windows·npm
人生导师yxc3 小时前
Java中Mock的写法
java·开发语言
lypzcgf3 小时前
Coze源码分析-资源库-编辑数据库-前端源码-核心组件
前端·数据库·源码分析·coze·coze源码分析·ai应用平台·agent平台
青岛少儿编程-王老师3 小时前
CCF编程能力等级认证GESP—C++5级—20250927
java·数据结构·c++
毕设源码-郭学长3 小时前
【开题答辩全过程】以 办公管理系统为例,包含答辩的问题和答案
java·eclipse
勤奋菲菲3 小时前
Koa.js 完全指南:下一代 Node.js Web 框架
前端·javascript·node.js
脑子慢且灵3 小时前
C语言与Java语言编译过程及文件类型
java·c语言·开发语言·汇编·编辑器