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
相关推荐
lanyxp5 分钟前
Sentinel 管不到 SQL 这一层——我写了个 MyBatis SQL 熔断器小闹5495 分钟前
Docker 如何才能学的更扎实XovH17 分钟前
MySQL 系列:第10篇 存储过程与自定义函数XovH17 分钟前
MySQL 系列:第8篇 子查询与集合操作XovH17 分钟前
MySQL 系列:第9篇 视图——定制化数据窗口慧一居士22 分钟前
SpringCloud 微服务Feigin 用的完整调用端和被调用的示例vortex527 分钟前
新手前后端开发学习指南:从Flask框架到全栈实践CodeStats29 分钟前
【虚拟机】 从 CPU 指令到虚拟机隔离:虚拟机就是一个“模拟了完整硬件的普通进程”我命由我1234529 分钟前
Jetpack Room - Room 查询返回列表无需判空、LIKE 关键字leeyi37 分钟前
Retriever 组件:让 Agent 学会「翻资料」的统一接口