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
相关推荐
葫芦和十三12 小时前
图解 MongoDB 07|索引类型:七种索引,七种访问形状朦胧之13 小时前
AI 编程-老项目改造篇爱勇宝16 小时前
我做了一个只用来搜歌词的小 AppIT_陈寒17 小时前
SpringBoot自动配置坑了我一晚上,原来问题出在这SelectDB18 小时前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台SelectDB18 小时前
秒级弹性、最高降本 70%:SelectDB Serverless 如何重塑云数仓资源效率程序猿大帅18 小时前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑PinkSun18 小时前
Spring AI ChatMemory踩坑实录:重启丢数据、Agent丢记忆、对话溢出壹方秘境18 小时前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具神秘面具男18 小时前
HarmonyOS 6.0跨端远程控制