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
相关推荐
云烟成雨TD30 分钟前
Spring AI Alibaba 1.x 系列【22】Agent 并行工具执行与超时 / 协作式取消实战段小二1 小时前
服务一重启全丢了——Spring AI Alibaba Agent 三层持久化完整方案UIUV1 小时前
Go语言入门到精通学习笔记lizhongxuan1 小时前
开发 Agent 的坑段小二1 小时前
Agent 自动把机票改错了,推理完全正确——这才是真正的风险itjinyin1 小时前
ShardingSphere-jdbc 5.5.0 + spring boot 基础配置 - 实战篇丶小鱼丶1 小时前
Java虚拟机【JVM】csdn2015_1 小时前
IDEA配置Continue张涛酱1074562 小时前
Subagent Orchestration 深入解析:多Agent协作的层级架构