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
相关推荐
大模型码小白1 小时前
JAVA 集合框架进阶:List 与 Set 的深度解析与实战IT_陈寒1 小时前
为什么我的JavaScript异步代码总是不按顺序执行?星栈2 小时前
Node 框架怎么选?Express、Koa、Egg、NestJS 场景化选型指南名字还没想好☜2 小时前
Go 的 time.Ticker 陷阱:定时任务里被忽略的内存泄漏与正确关闭锋行天下2 小时前
打造企业内部知识库系统RAG全栈项目音符犹如代码3 小时前
后端视角看 EventBus:发布订阅总线的原理、场景与用法前端炒粉3 小时前
手撕小汇总码事漫谈3 小时前
Kimi K3 真实体验:全网评价整理,优缺点一次性说清楚Fanta丶3 小时前
16.Activiti8 SpringBoot3.X 部署与测试用户208046804563 小时前
Python3 数据类型转换新手实战指南