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
相关推荐
ZHOU_WUYI2 分钟前
Flask与Celery 项目应用(shared_task使用)忠于明白2 分钟前
Spring AI 核心工作流陈小桔6 分钟前
限流算法java实现黑客老李10 分钟前
JavaSec | SpringAOP 链学习分析勤奋的知更鸟36 分钟前
Java编程之原型模式冒泡的肥皂39 分钟前
强大的ANTLR4语法解析器入门demo叶 落1 小时前
[Java 基础]数组KK溜了溜了1 小时前
JAVA-springboot log日志珂朵莉MM1 小时前
2021 RoboCom 世界机器人开发者大赛-高职组(初赛)解题报告 | 珂学家香蕉炒肉1 小时前
Java优化:双重for循环