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 小时前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级凌虚5 小时前
面向 MySQL 用户的 PostgreSQL 快速上手指南都叫我大帅哥6 小时前
Java JJWT 完全指南:从入门到生产级实践IT_陈寒6 小时前
Vite静态资源引用这个坑我踩得有点疼前端开发张小七7 小时前
Java 学习笔记 · 第一课:基础语法与核心概念海盗12347 小时前
微软技术周报 ——2026-08-03沙湖遇雨7 小时前
6. Pipeline 的生命周期青青子衿悠悠我心7 小时前
TRAE Work让财务月底对账3小时变5分钟创安电气研究所7 小时前
用TypeScript给Modbus设备做一层适配器妙码生花7 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(五十):增加管理员角色组管理