【Spring】@Component 和 @Configuration 对比

@Configuration

java 复制代码
@Configuration
public class ComponentTest {

    @Bean
    public String test(){
        return new String("123");
    }

    @Bean
    public String test2(){
        return test();
    }
}

Test:

java 复制代码
public class ComponentBeanTest extends BaseTest{

    @Resource
    String test;

    @Resource
    String test2;

    @Test
    public void test() {
        System.out.println(test);
        System.out.println(test2);
        System.out.println(test == test2);
    }
}

输出:

复制代码
123
123
true

@Component

java 复制代码
@Component
public class ComponentTest {

    @Bean
    public String test(){
        return new String("123");
    }

    @Bean
    public String test2(){
        return test();
    }
}

Test和@Configuration注解的相同

输出:

复制代码
123
123
false
相关推荐
梦梦代码精1 分钟前
开源、免费、可商用:BuildingAI一站式体验报告
开发语言·前端·数据结构·人工智能·后端·开源·知识图谱
百炼成神 LV@菜哥8 分钟前
Kylin Linux V10 aarch64安装DBeaver
java·linux·服务器·kylin
有代理ip23 分钟前
成功请求的密码:HTTP 2 开头响应码深度解析
java·大数据·python·算法·php
好好沉淀44 分钟前
ES 脚本核心语法:ctx._source [‘group_id‘]
java·elasticsearch·script
李慕婉学姐1 小时前
【开题答辩过程】以《基于Spring Boot的疗养院理疗管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端
tb_first1 小时前
SSM速通2
java·javascript·后端
qq_12498707531 小时前
基于协同过滤算法的运动场馆服务平台设计与实现(源码+论文+部署+安装)
java·大数据·数据库·人工智能·spring boot·毕业设计·计算机毕业设计
大飞哥~BigFei1 小时前
自定义注解记录接口切面log日志入库优化
java
人道领域1 小时前
javaWeb从入门到进阶(maven高级进阶)
java·spring·maven
一路向北⁢1 小时前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(一)
java·spring boot·后端·sse·通信