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亨H几秒前
一文读懂计算机系统核心流量术语q***064710 分钟前
SpringSecurity相关jar包的介绍rafael(一只小鱼)18 分钟前
AI运维开发平台学习空空kkk28 分钟前
SpringMVC——IO笔记陈橘又青1 小时前
100% AI 写的开源项目三周多已获得 800 star 了lcu1111 小时前
Java 学习40:继承p***q781 小时前
【保姆级教程】apache-tomcat的安装配置教程大云计算机毕设1 小时前
【2026计算机毕设选题】计算机毕设全新推荐项目选题指南(70+精选热门方向)2501_941148611 小时前
C++实时数据处理实战:多线程与异步IO结合高性能代码解析u***u6851 小时前
C++在系统中的异常处理