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 小时前
高可用读写分离实战(二):我把数据库主库停了,结果整个集群的反应和我想象的不一样掘金者阿豪1 小时前
《高可用读写分离集群实战》系列(一)Dilee2 小时前
Spring AI 2.0.0 Prompt 最小 Demo:system、user、template 到底怎么分工未秃头的程序猿2 小时前
Java 26正式发布!这3个新特性,让代码量直接减半小旭Coding2 小时前
卧靠!Go 传给前端的 int64 竟然变成了这个?用户298698530142 小时前
Word 文档文本查找与替换的 Java 实现方案阿哉2 小时前
Nacos 服务发现源码:藏在背后的两套事件机制,90%的人只讲了一半