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
相关推荐
程序员cxuan11 分钟前
再见 Claude,国模 SOTA 模型来了程序员cxuan15 分钟前
夯爆了,影视界的 Codex 来了!孟健30 分钟前
抖音官方 CLI 上线:一句话搜同行爆款、扒文案,实测全记录小手cool40 分钟前
二分查找:在有序数组中查找最接近 target 的元素索引烬中寻道1 小时前
GoFrame + Trae:把数据库凭据从配置文件里彻底拿掉李白的手机1 小时前
深入理解 Spring @RefreshScope:从入门用法到源码原理,再到举一反三实战扩展深入云栈1 小时前
Netty 4.2.x 源码深度解析 (二十五):PoolThreadCache 与引用计数 —— 线程缓存与内存泄漏检测砚底藏山河2 小时前
量化行情横截面中性化闭式与回归实现想不到一个好的ID2 小时前
Redis 常见数据类型与应用场景