package com.example.demo;
import com.example.demo.service.MyService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
//指定bean 所在的包
AnnotationConfigApplicationContext context
= new AnnotationConfigApplicationContext("com.example.demo");
// context.refresh(); // 初始化 Spring 容器
// ... 获取和使用 Bean ...
// context.close();
MyService bean1 = context.getBean(MyService.class);
MyService bean2 = context.getBean(MyService.class);
System.out.println("bean1 hashCode: " + bean1.hashCode());
System.out.println("bean2 hashCode: " + bean2.hashCode());
if (bean1 == bean2) {
System.out.println("bean1 and bean2 are the same instance.");
} else {
System.out.println("bean1 and bean2 are not the same instance.");
}
}
}
spring 获取ioc容器,从容器中获取bean
cuilll2024-07-02 14:49
相关推荐
杨杨杨大侠4 小时前
都能接模型、RAG 和 MCP,Java Agent 框架到底差在哪?凤山老林5 小时前
Spring Boot 启动冲刺:耗时剖析、懒加载策略与事件驱动初始化优化Thinker QAQ5 小时前
并发编程(九):BlockingQueue 与 Channel——语言层的原子性、可见性与有序性朱涛的自习室6 小时前
我给 Gemini 4 和 DeepSeek 搞了套「末位淘汰」,AI 干活会更猛吗?(Day 3)霸道流氓气质7 小时前
RAG 可观测性与生产监控实战:四层遥测模型、五大仪表盘与 OpenTelemetry 全链路追踪的 Java 生产级实现AOI小白新手上路7 小时前
autodl常用的tar 打包下载(数据集)命令手册:逐段拆解与套用模板woniu_maggie8 小时前
SAP MDG主数据治理实战:从架构原理到项目实施全解析xiaoqiMikko8 小时前
Redis 实战(一):内存满了 —— 拒写、淘汰、被系统杀掉,以及删了 key 内存为什么不降(6.2.4 实测)dishugj8 小时前
系统架构风险点、敏感点和权衡点的概念和区分