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 小时前
Arthas 实战:线上“看 Map”的四种武器烂蜻蜓2 小时前
Flask入门教程(二十六):Session API——用户会话状态管理霸道流氓气质2 小时前
Spring AI异常处理与重试机制郑州光合科技余经理4 小时前
本地生活服务系统:成品模块和定制接口怎么划界萧瑟余晖4 小时前
Java深入解析篇三十三之密封类与接口(Sealed Classes)详解论迹复利7 小时前
FreeRTOS 在 RISC-V 上是如何“点火“的 —— 从 main() 到第一个任务的完整链路小江的记录本8 小时前
【CSS】CSS 动画:transition、animation、transform、CSS3 新特性(附《思维导图》)张宇Joaquin8 小时前
鲲鹏统一并行加速库KUPL--众核并行能力介绍宸津-代码粉碎机8 小时前
AI攻防战升级!基于Spring AI构建Java应用自动免疫安全体系2601_963749109 小时前
越华环保集团数字化污水治理:端边云采集架构与平台对接实现