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
相关推荐
蒸蒸yyyyzwd3 小时前
cpp对象模型学习笔记1.1-2.8qq_297574674 小时前
【实战教程】SpringBoot 集成阿里云短信服务实现验证码发送程序员徐师兄4 小时前
Windows JDK11 下载安装教程,适合新手RANCE_atttackkk4 小时前
[Java]实现使用邮箱找回密码的功能五岳5 小时前
DTS按业务场景批量迁移阿里云MySQL表实战(下):迁移管理平台设计与实现韩立学长5 小时前
【开题答辩实录分享】以《智能大学宿舍管理系统的设计与实现》为例进行选题答辩实录分享zhougl9965 小时前
Java 所有关键字及规范分类Python 老手5 小时前
Python while 循环 极简核心讲解java1234_小锋6 小时前
Java高频面试题:MyISAM索引与InnoDB索引的区别?Mr_Xuhhh6 小时前
MySQL函数详解:日期、字符串、数学及其他常用函数