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
相关推荐
老马识途2.01 小时前
关于跨域问题的总结都叫我大帅哥2 小时前
Java日期时间三十年战争:从Date考古到LocalDateTime革命,以及数据库与前端的那点事儿Muscleheng2 小时前
SpringBoot 集成 DeepSeek 实现 RAG 文档问答2501_936415692 小时前
可变参数&综合练习&斗地主游戏颜酱3 小时前
07 | 把字段与指标同步到 Qdrant(生成阶段)Larcher3 小时前
从“加载模型”界面到端侧推理:拆解一个 React + WebGPU 大模型 Demo极客先躯3 小时前
高级java每日一道面试题-2026年05月03日-实战篇[Docker]-如何实现容器化环境的数据加密?xqqxqxxq4 小时前
Java Socket 多人聊天室(私聊+群聊)技术笔记(V4版本)Larcher4 小时前
从状态快照到惰性初始化:读懂 React useState 的三个关键场景lazy H4 小时前
Git clone 怎么用?克隆项目及常见问题完整教程