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
相关推荐
蜗牛互联网3 小时前
WSL Containers GA:本地AI容器的生命周期、网络与治理验收龙亘川3 小时前
铭记英烈守初心,数字强基促服务:亘川智城退役军人服务系统的实践与价值OxYGC4 小时前
[AI工程] Spring AI 第廿一篇:存量 REST 接口的 MCP 化改造实录——工具从哪条路径注册、参数描述怎么写、身份怎么过去Zhou1411364 小时前
SpringBoot_02_自动配置原理FYKJ_20104 小时前
SSM校园失物招领系统41452-计算机课程设计、毕业设计kv1104 小时前
Android studio国内开发有关卓怡学长5 小时前
w198基于springboot始于足下健康打卡平台的设计与实现阿狗童鞋5 小时前
Java并发编程实战指南子兮曰5 小时前
Node.js 50个优势场景盘点:一个人单干,为啥我多数时候只用它IT_陈寒5 小时前
我花3小时debug,就因为JavaScript这个隐式转换坑