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
相关推荐
星栈2 分钟前
从装一堆工具到看懂 Node 工程化思维:我的项目复盘记录65岁退休Coder21 分钟前
LangChain v1.3.4 笔记 - 05 Agent 上下文记忆颜酱25 分钟前
04 | 召回前置准备:搭好召回所需的四个数据库JaneConan36 分钟前
鸿蒙 韶非 UI 系列:能力调用 startAbilityForResult,跳能力拿回参,鸿蒙能力路由入门晴空了无痕1 小时前
从 Go 基础到 K8s:一条可落地的 Go 服务端成长路线用户8356290780511 小时前
如何使用 Python 在 Excel 中添加、编辑和删除超链接神明不懂浪漫1 小时前
【第五章】Java中的继承与多态花椒技术1 小时前
原本要 2 天的服务端冒烟前置审查,为什么 3 分半就能出报告?|QA 质量交付实践(三)达达尼昂1 小时前
AI 编程的工程化实践:Flutter AI Harness 的设计与落地IT_陈寒2 小时前
React的useEffect依赖项把我坑惨了