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
相关推荐
长栎43 分钟前
写 for 循环写了十年,你却从没用过迭代器模式最狠的那一面LiaCode1 小时前
Redis 在生产项目的使用用户559822481221 小时前
Docker Compose Down 导致容器数据误删——ext4 日志恢复全记录LiaCode1 小时前
一天学完 redis 的爽翻版核心知识总结大刚测试开发实战1 小时前
如何内网穿透访问本地私有化部署的TestHubxiaodaoluanzha1 小时前
迄今為止,最簡單的編程語言 NolangCsvn1 小时前
Docker 容器管理入门 — 从镜像到容器编排用户762352425911 小时前
ShardingJDBC行者全栈架构师1 小时前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法令人头秃的代码0_01 小时前
mac(m5)平台编译openjdk