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
相关推荐
明月_清风3 小时前
Muse 登顶 App Store 第一,SDK 直接开源:AI Agent 开始进入下一个阶段leikooo3 小时前
用餐厅和盘子理解栈和堆Ivanqhz4 小时前
激活函数在 Transformer 中的作用及各种变体简述沙漠之主4 小时前
C++编程教学设计资料:从入门到实战的完整课程方案FPGA小徐4 小时前
PA1 基础设施(2)深度解析:NEMU 中 ITRACE、MTRACE、FTRACE、IRINGBUF、ETRACE、DIFFTEST 全解析YOLO数据集集合5 小时前
EvoAgent:面向PR研发治理的自进化Multi-Agent Harness系统hsfxuebao5 小时前
Loop Engineering 保姆级教程 + 项目实战知行EDI5 小时前
知行之桥 MaBang 端口使用指南——Create Order 订单创建篇笑鸿的学习笔记5 小时前
C++笔记之大块顺序写打工仔折腾 AI5 小时前
从 Demo 到生产级 Agent:8 个关键设计机制与 Python 实现拆解