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
相关推荐
小雅痞7 分钟前
[Java][Leetcode middle] 55. 跳跃游戏com未来12 分钟前
使用 NSSM 安装 Tomcat 11.0.6 为 Windows 服务TDengine (老段)18 分钟前
基于 TSBS 标准数据集下 TimescaleDB、InfluxDB 与 TDengine 性能对比测试报告养军博客20 分钟前
spring boot3.0自定义校验注解:文章状态校验示例lgily-122521 分钟前
常用的设计模式详解IT成长史26 分钟前
deepseek梳理java高级开发工程师微服务面试题茶本无香26 分钟前
Feign+Resilience4j实现微服务熔断机制:原理与实战遇见火星27 分钟前
Ansible模块——从控制节点向目标主机复制文件!小码ssim30 分钟前
通过POI实现对word基于书签的内容替换、删除、插入香饽饽~、44 分钟前
函数式方法的实现(JDK8+)