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
相关推荐
倒流时光三十年10 小时前
PostgreSQL 中的 NULL 陷阱:从一次排除过滤说起xkxnq10 小时前
第七阶段:企业级项目实战核心能力(118天)Vue项目缓存策略:接口缓存(内存+本地)+ 组件缓存+路由缓存组合方案代码改善世界10 小时前
【C++进阶】二叉搜索树学习3人组10 小时前
业务主表+JSON自定义字段雨落在了我的手上11 小时前
初识java(六):方法的使用张敬之、11 小时前
sa-token_Evan_Yao11 小时前
从“全量发布”到“小步快跑”:灰度发布的简单实践与学习路径想带你从多云到转晴11 小时前
优选算法---双指针闲适达人11 小时前
nginx传递url的获取方案石小石Orz11 小时前
给Claude增加状态栏显示:claude-hud保姆级教程