专栏名称: 码农小胖哥
技术公众号:码农小胖哥
目录
相关文章推荐
51好读  ›  专栏  ›  码农小胖哥

从Spring 应用上下文获取 Bean 的常用姿势

码农小胖哥  · 掘金  ·  · 2019-11-05 22:00

正文

阅读 1

从Spring 应用上下文获取 Bean 的常用姿势

1. 前言

通常,在Spring应用程序中,当我们使用 @Bean @Service @Controller @Configuration 或者其它特定的注解将 Bean 注入 Spring IoC 。然后我们可以使用 Spring 框架提供的 @Autowired 或者 JSR250 JSR330 规范注解来使用由 Spring IoC 管理的 Bean

2. 从应用程序上下文中获取 Bean

今天我们将来学习如何从 ApplicationContext 中获取 Bean 。因为有些情况下我们不得不从应用程序上下文中来获取 Bean

2.1 获取所有的 Bean

ApplicationContext 提供了获取所有已经成功注入 Spring IoC 容器的 Bean 名称的方法 getBeanDefinitionNames() 。然后我们可以借助于其 getBean(String name) 方法使用 Bean 名称获取特定的 Bean 。 我们使用之前 文章 中介绍的 CommandLineRunner 接口来打印一下结果。

 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.ApplicationContext;
 
 import java.util.stream.Stream;
 
 /**
  * @author Felordcn
  */
 @SpringBootApplication
 public class WarSpringBootApplication implements CommandLineRunner {
     @Autowired
     private ApplicationContext applicationContext;
 
     public static void main(String[] args) {
         SpringApplication.run(WarSpringBootApplication.class, args);
 
 
     }
 
     @Override
     public void run(String... args) throws Exception {
         String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
 
         Stream.of(beanDefinitionNames).forEach(beanName->{
             System.out.println("beanName : " + beanName);
 
             Object bean = applicationContext.getBean(beanName);
 
             System.out.println("Spring bean : " + bean);
         });
 
     }
 }复制代码

运行应用会输出:

 2019-11-05 22:15:54.392  INFO 6356 --- [           main] cn.felord.war.WarSpringBootApplication   : Started WarSpringBootApplication in 4.663 seconds (JVM running for 7.58)
 beanName : org.springframework.context.annotation.internalConfigurationAnnotationProcessor
 Spring bean : org.springframework.context.annotation.ConfigurationClassPostProcessor@6c44052e
 beanName : org.springframework.context.annotation.internalAutowiredAnnotationProcessor
 Spring bean : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@5c371e13
 beanName : org.springframework.context.annotation.internalCommonAnnotationProcessor
 Spring bean : org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@530a8454
 beanName : org.springframework.context.event.internalEventListenerProcessor
 Spring bean : org.springframework.context.event.EventListenerMethodProcessor@1e34c607
 beanName : org.springframework.context.event.internalEventListenerFactory
 Spring bean : org.springframework.context.event.DefaultEventListenerFactory@5215cd9a
 beanName : fooController
 Spring bean : cn.felord.war.controller.FooController@31198ceb
 beanName : IServiceImpl
 Spring bean : cn.felord.war.controller.IServiceImpl@51671b08
  <more...>复制代码






请到「今天看啥」查看全文