开发者

Spring Boot中获取IOC容器的多种方式

开发者 https://www.devze.com 2025-09-24 11:22 出处:网络 作者: 生无谓
目录1. 直接注入ApplicationContext2. 实现ApplicationContextAware接口3. 通过SpringApplication.run()获取ApplicationContext4. 通过BeanFactory获取5. 通过实现BeanFactoryAware接口1. 直接注入ApplicationContex
目录
  • 1. 直接注入ApplicationContext
  • 2. 实现ApplicationContextAware接口
  • 3. 通过SpringApplication.run()获取ApplicationContext
  • 4. 通过BeanFactory获取
  • 5. 通过实现BeanFactoryAware接口

1. 直接注入ApplicationContext

@Service
public class MyService {
    
    @Autowired
    private ApplicationContext applicationContext;
    
    public编程客栈 void DOSomething() {
        // 通过ApplicationContext获取Bean
        UserService userService = applicationContext.getBean(UserService.class);
        // 或者通过名称获取
        UserService userService2 = (UserService) applicationContext.getBean("userService");
    }
}

2. 实现ApplicationContextAware接口

@Component
public class SpringContextUtil implements ApplicationContextAware {
    
    private static ApplicationContext applicationContext;
    
    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }
    
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }
    
    public static <T> T getBean(String name, Class<T> clazz) {
        return applicationContext.getBean(name, clazz);
    }
    
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }
}

3. 通过SpringApplication.run()获取ApplicationContext

@SpringBootApplication
public class Application {
    
    public static void main(String[] args) {
        // 启动应用并获取ApplicationContextwww.devze.com
    android    ApplicationContext context = SpringApplication.run(Application.class, args);
        
        // 直接从context中获取Bean
        UserService userServipythonce = context.getBean(UserService.class);
        userService.doSomething();
    }
}

4. 通过BeanFactory获取

@Service
public class MyService {
    
    @Autowired
    private BeanFactory beanFactory;
    
    public void doSomething() {
        if (beanFactory.containsBean("userService")) {
            UserService userService = (UserService) beanFactory.getBean("userService");
            // 使用userService
        }
    }
}

5. 通过实现BeanFactoryAware接口

@Component
public class MyComponent implements BeanFactoryAware {
    
    private BeanFactory beanFactory;
    
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
    }
    
    public void doSomething() {
        UserService userService = (UserService) beanFactory.getBean("userService");
        // 使用Bean
    }
}

到此这篇关于Spring Boot中获取IOC容器的多种方式的文章就介绍到这了,更多相关SpringBoot 获取IOC容器内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.cpandroidpcns.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号