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
相关推荐
linmengmeng_13142 分钟前
【总结】MyBatis-Plus批量插入与清表的正确姿势高级程序源2 分钟前
django大学生创新创业项目管理系统94923-计算机课程设计、毕业设计木井巳7 分钟前
【记忆化搜索】不同路径Sam_Deep_Thinking7 分钟前
new Thread()之后发生了什么?泡海椒8 分钟前
规则引擎对比:JQuick-Java vs Drools 轻量场景选型对比wno70427 分钟前
Spring Security短信验证码登录步行cgn35 分钟前
Spring util 命名空间详解古法安卓1 小时前
Android-休眠唤醒后onLocationChanged没有数据问题排查打工仔折腾 AI1 小时前
普通摄像头接入AI识别:绿联NAS部署Frigate监控实战周GZ2 小时前
简单讲解线程池的4种拒绝策略