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
相关推荐
Joy T2 小时前
Agent 确定性上下文设计:从 Prompt 参数注入到能力分层m0_587383005 小时前
广州 24 小时自助健身房解决方案技术实现与实战指南·云扬·5 小时前
Prompt 工程与安全:从 System 设计到注入防御代码不停6 小时前
自动化测试(java)行百里er6 小时前
Redis 完结篇——Redis 热点问题在 Spring Boot 项目中实战唠点键盘之外的7 小时前
26 向量数据库实战:从 0 搭一条能用的检索链路(pgvector + Spring AI)高频因子挖掘机7 小时前
只取最近 60 个交易日,却每次拉全历史:量化脚本变慢的原因与改法鬼手点金8 小时前
opencode-rules 插件孟健8 小时前
从5000亿Token到83%字节匹配:AI逆向工程与重构的工程闭环实践paopaokaka_luck9 小时前
滑雪场管理系统(AI智能助手流式对话,ECharts 运营数据分析、教练、雪具与场地一体化预约,钱包支付退款、订单核销与服务评价)