Spring如何应用上下文获取Bean

发布时间:2020-07-23 09:53:35 作者:小猪
来源:亿速云 阅读:338

小编这次要给大家分享的是Spring如何应用上下文获取Bean,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

1. 前言

通常,在Spring应用程序中,当我们使用 @Bean@Service@Controller@Configuration 或者其它特定的注解将 Bean 注入 Spring IoC 。然后我们可以使用 Spring 框架提供的 @Autowired 或者 JSR250JSR330 规范注解来使用由 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...>

2.2 通过名称获取特定的 Bean

从上面打印的信息我们也能看出来一些端倪。

但是请注意:如果你在声明 Bean 的时候指定了名称就只是你指定的名称 。如果我们熟悉这些规则,使用上面提到的getBean(String name) 方法不失为一种好办法。

2.3 通过类型来获取 Bean

如果我们不清楚我们想要的特定类型 Bean 的名称,我们可以根据类型来获取 BeanApplicationContext 提供了可以加载特定类型的 Bean 的所有 Bean 的方法getBeansOfType()。它将返回 Map <String,Object> 其中键是 Bean 名称,而值是 Bean 的实际对象。

我们修改 2.1 章节 例子中的 run 方法:

   @Override
   public void run(String... args) throws Exception {
     Map<String, FooController> beansOfType = applicationContext.getBeansOfType(FooController.class);
 
 
     beansOfType.forEach((beanName,bean)->{
       System.out.println("beanName : " + beanName);
       System.out.println("bean : " + bean);
     });
   }

再次运行,控制台打印出:

beanName : fooController
 bean : cn.felord.war.controller.FooController@545f80bf

2.4 获取特定 Bean 声明注解标记的 Bean

ApplicationContextgetBeansWithAnnotation() 方法可以让我们获取 @Service@Controller或任何其它可以用来创建 Bean 的注解创建的 Bean

   @Override
   public void run(String... args) throws Exception {
     Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(Controller.class);
 
     beansWithAnnotation.forEach((beanName,bean)->{
       System.out.println("beanName : " + beanName);
       System.out.println("bean : " + bean);
     });
   }

打印出:

beanName : fooController
 bean : cn.felord.war.controller.FooController@18ca3c62
 beanName : basicErrorController
 bean : org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController@2c0f7678

3. 总结

在本文中,我们学习如何从 Spring 应用上下文中获取所有 Bean 的列表。有时我们需要检查我们期望的 Bean 是否在 Spring 上下文中加载,或者我们需要检查 Spring IoC 声明的特定的 Bean 。当然你可以开启Spring Boot Actuator 的 beans 端点来获取所有的 Bean 信息。

看完这篇关于Spring如何应用上下文获取Bean的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

推荐阅读:
  1. Spring实战之让Bean获取Spring容器操作示例
  2. 详解在SpringBoot应用中获取应用上下文方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring bean bea

上一篇:PHPCMS中PNG水印不显示怎么办

下一篇:什么是ssm框架原理及流程

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》