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
相关推荐
aXin_ya6 分钟前
乐尚代驾,总结仙俊红11 分钟前
Java JUC:CompletableFuture 详解,多个任务并行执行并等待全部完成JAVA面经实录91714 分钟前
MongoDB(文档型 NoSQL)cfm_291414 分钟前
JVM类加载机制初步了解让我上个超影吧16 分钟前
Cluade code:上下文压缩plainGeekDev19 分钟前
批量写入 → Room 事务宋哥转AI19 分钟前
MCP 第一天我没写@Tool,先在一个大仓库里划这三层填满你的记忆21 分钟前
MCP协议是什么?为什么它被称为AI时代的“USB接口”?云烟成雨TD21 分钟前
Spring AI Alibaba 1.x 系列【72】集成 MCP 客户端独隅25 分钟前
Visual Studio Code 和 Visual Studio 2026 两大开发工具的核心差异