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
相关推荐
苍何5 小时前
开源微信流 Windows,微信聊天记录,可以直接给 Codex 和 Obsidan 了代码什么用6 小时前
Spring基础使用代码什么用6 小时前
Spring对IoC的实现Su米苏6 小时前
Spring AI 中MCP 与普通 @Tool的区别明月_清风6 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编码云数智-园园7 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析老板一杯拿铁7 小时前
Java 异常体系详解:从字节码原理到最佳实践蜗牛互联网7 小时前
Java 17 HttpClient调用文件转写API的超时与失败回退禾小西7 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?我不是阵雨7 小时前
JDK 21虚拟线程Pinning陷阱:一文拔钉解困