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
相关推荐
王中阳Go几秒前
告别调包侠!2026年Go/Java程序员的AI架构师实战转型指南⑩-2 分钟前
SpringCloud-Feign&RestTemplate培培说证4 分钟前
2026大专Java开发工程师,考什么证加分?我是谁的程序员5 分钟前
抓包工具有哪些?代理抓包、数据流抓包、拦截转发工具qq_336313935 分钟前
java基础-方法引用总是学不会.8 分钟前
【JUC编程】一、线程的基础概念由之18 分钟前
Spring事件监听机制简单使用小鸡吃米…20 分钟前
Python - 类属性沉下去,苦磨练!22 分钟前
计算一个字符串在另一个字符串中出现次数开心猴爷37 分钟前
APP 上架苹果 App Store 被拒,并不总是产品问题