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 小时前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径城管不管2 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别IT小白杨2 小时前
从环境制备到自动化工作流:多账号运营的工程化架构拆解豆瓣鸡3 小时前
算法日记 - Day3萧瑟余晖3 小时前
Java深入解析篇九之NIO详解mldong3 小时前
从 mldong 到 jeeflow:一个工作流引擎的独立进化The Chosen One9853 小时前
高进度算法模板速记(待完善)陈随易3 小时前
MoonBit抓包模块pcap,查看电脑的每一次联网通信Aaron - Wistron4 小时前
Web API C# (Furion版)带 单元测试卷无止境4 小时前
写代码这件事,到底该讲究点什么?