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
相关推荐
y = xⁿ6 小时前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现东风破_7 小时前
danci 项目(一):从需求到架构,一个单词学习系统为什么会这样设计侧耳倾听1117 小时前
jwt使用简介顶点多余7 小时前
那些在算法中适合巩固的知识点---1AI人工智能+电脑小能手8 小时前
大白话说Java设计模式-46-责任链模式(业务实战篇)jay神8 小时前
【计算机毕业设计】基于SpringBoot的程序教学辅助系统2601_962066498 小时前
【Sql Server】Update中的From语句,以及常见更新操作方式yaoxin5211239 小时前
507. Java 反射 - 在 BeanFactory 中实现依赖注入OPEN-F10 小时前
C++模板教程:变参模板、折叠表达式与SFINAE2601_9620722111 小时前
SpringMVC进阶(自定义拦截器以及异常处理)