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
相关推荐
chuan.bai3 小时前
Java RAG 实战(第 11 篇):RAG 知识工作台网页0x535 小时前
网站通信(一)To_OC5 小时前
Next.js + Redis 构建笔记系统:Redis Hash 实战与性能优化Terra.K5 小时前
Java异常学习[特殊字符]葡萄城技术团队7 小时前
InfluxDB 2\.x 深度解析:核心架构、Flux 函数与制造业落地指南(三)Super 含7 小时前
Android 启动优化(五):线程、GC 与 IO 为什么会拖慢启动?counting money7 小时前
Java IO流详解:从InputStream到文件操作实战程序员小八7778 小时前
上海百度B端java后端日常实习一面北斗落凡尘8 小时前
LangGraph 入门实战(11)--输出模式demo007x9 小时前
Hermes-Agent 技术架构