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
相关推荐
Emily156853598701 小时前
Emerson 1C31129G03 Analog Input Module就叫_这个吧6 小时前
Java递归方法实现面包屑导航fīɡЙtīиɡ ℡6 小时前
AI 应用系统设计城管不管6 小时前
重生——第十一次面试之挖财一面2026.8.19已OC码匠许师傅6 小时前
【C++ 面试真题】26. 聊聊 C++ 的智能指针AI绘画哇哒哒6 小时前
【建议收藏!】35岁后端血泪忠告,这3类人别硬转Agent(过来人亲述)最强小杰7 小时前
gpt-5.6-sol 频繁报 503 怎么办?区分容量熔断和限速 429 的排查方法 + 可复用 retry wrapper唐青枫7 小时前
一个值多种形态:Zig union、Tagged Union 与内存布局实战卷无止境7 小时前
Windows 上丝滑开发 Python,并稳定构建 Docker 镜像Nturmoils8 小时前
备份完不算完,先还原到临时库验一遍