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
相关推荐
吴声子夜歌6 小时前
Java面试——算法evans在进步6 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解凤山老林7 小时前
Spring Boot 3.x AOT 编译实战:从原理、踩坑到生产落地博傅8 小时前
Spring 核心原理yurenpai(27届找实习中)8 小时前
从零读懂 AI 智能客服(一):模块职责与 SSE 聊天链路(后端架构)用户938515635078 小时前
掘金风格后端数据库设计实战:从用户表到分布式架构,一文搞定陈皮波比茶8 小时前
Swagger玹外之音8 小时前
Spring AI + Elasticsearch 向量存储实战:从零构建智能文档检索系统拾光师8 小时前
Hadoop 三种运行模式:从单机调试到生产集群,一路搭过来小闫BI设源码8 小时前
Elasticsearch面试必看:如何让客户端精准选择节点高效执行请求?