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
相关推荐
武子康9 小时前
大数据-210 如何在Scikit-Learn中实现逻辑回归及正则化详解(L1与L2)Tao____9 小时前
如何对接Modbus-tcp协议(使用Thinlinks物联网平台)鱼跃鹰飞10 小时前
经典面试题:K8S的自动缩扩容和崩溃恢复Coder_Boy_10 小时前
Spring Boot 事务回滚异常 UnexpectedRollbackException 详解(常见问题集合)青云交10 小时前
Java 大视界 -- 基于 Java+Redis Cluster 构建分布式缓存系统:实战与一致性保障(444)风象南10 小时前
SpringBoot 实现网络限速不知疲倦的仄仄10 小时前
第五天:深度解密 Netty ByteBuf:高性能 IO 的基石xiaobaishuoAI10 小时前
后端工程化实战指南:从规范到自动化,打造高效协作体系源代码•宸10 小时前
Golang语法进阶(定时器)期待のcode10 小时前
TransactionManager