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
相关推荐
明月_清风6 小时前
加密解密系统完全指南:原理剖析与 Go 实践方也_arkling6 小时前
【Java-Day08】static / final / 枚举橙淮6 小时前
Spring Bean作用域与生命周期全解析Chengbei117 小时前
一站式源码安全检测工具、云安全 / APP / 小程序源码敏感信息递归多层目录扫描AK、JWT、手机号、身份证等敏感信息llz_1127 小时前
web-第一次课后作业秋97 小时前
Java项目运行5天左右自动宕机:系统性定位与解决方案小江的记录本7 小时前
【JVM虚拟机】垃圾回收GC:垃圾收集器:CMS:核心原理、回收流程、优缺点、废弃原因(附《思维导图》+《面试高频考点清单》)DIY源码阁7 小时前
JavaSwing学生成绩管理系统 - MySQL版冬奇Lab8 小时前
每日一个开源项目(第105篇):Twenty - 跳出 Salesforce 的圈套,定义现代开源 CRMbasketball6168 小时前
C++ NULL 和 nullptr 区别 以及 nullptr 的核心实现