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
相关推荐
实名上网宋凯宣8 小时前
Django天河归来9 小时前
第三方接口不稳定,如何让核心任务继续完成Knight_AL9 小时前
Java 文件下载时文件名中的 `#` 被转换成 `%23`?一次 URL 编码与解码问题排查利威尔xu10 小时前
Go IM 源码实战(14):本地缓存快 3000 倍,失效却难 10 倍逃逸线LOF10 小时前
Ribbon负载均衡原理(Irule组件、源码分析)砚底藏山河11 小时前
python量化自定义周期 K 线合成慢云智慧空间11 小时前
从设备智能到空间理解,空间智能终端解决方案商如何重新定义行业标准利威尔xu11 小时前
Go IM 源码实战(13):30 个会话只有 2 个红点,未读数错在三条路径猿究院_夏燊11 小时前
MySQL 的四种隔离级别薛定谔的猫-菜鸟程序员11 小时前
软件架构设计师开发资料大全(从入门到精通)