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
相关推荐
梦想很大很大30 分钟前
一句“你好”用了 5671 Token:我在 Workrun 中追查 Agent 隐性成本的过程坤岭36 分钟前
Agentic BI 落地实战SimonKing37 分钟前
AI Assistant被"隐藏"了?别慌,IDEA 2026.3 这波是换了个脑子Resean022342 分钟前
SpringMVC 6+源码分析(九)异常处理核桃er1 小时前
RuoYi-Vue 源码中的 Spring IoC 与 DI(两个实例)geats人山人海1 小时前
现代c++4.1小番茄程序猿1 小时前
一条 ALTER 拖死全表:MDL 阻塞的完整时间线(8.4.11 实测)北风toto1 小时前
Spring Boot 常用注解 · 概要笔记我不会起名字3221 小时前
仅 Web 主动 ping:30 秒读超时与各平台保活Mh2 小时前
AI 的模型能力越来越强,为什么我的编码能力越来越弱?