简体   繁体   English

我可以在用@PostConstruct 注释的方法中获取所有bean吗?

[英]Can I get all the beans in a method annotated with @PostConstruct?

As far as I know, A method annotated with @PostConstruct will be executed after its bean has been initialized.据我所知,用@PostConstruct 注释的方法将在其 bean 初始化后执行。

So can I get all the beans in this method?那么我可以用这个方法得到所有的豆子吗? Like this...像这样...

@CustomAnnotation
public class Foo {
}

@Service
public class TestBean {
   @Autowired
   private Application context;

   @PostContruct
   public void init() {
      // get all beans annotated with @CustomAnnotation
      context.getBeansWithAnnotation(CustomAnnotation.class);
      // to do something...
   }
}

If the TestBean is initialized before the Foo, does the Foo can be detected in init()?如果 TestBean 在 Foo 之前初始化,在 init() 中是否可以检测到 Foo?

Spring initialization has at least two distinct steps when it comes to singleton beans.对于单例 bean,Spring 初始化至少有两个不同的步骤。

Before the actual singleton bean instances are created, and your @PostConstruct method is called, the bean factory reads al the available configurations (eg XML files, Groovy scripts, @Configuration classes, other) and registers all the encountered bean definitions.在创建实际的单例 bean 实例和调用@PostConstruct方法之前,bean 工厂读取所有可用配置(例如 XML 文件、Groovy 脚本、 @Configuration Configuration 类等)并注册所有遇到的 bean 定义。

getBeansWithAnnotation() should find a Foo bean, if it wasn't created from it's bean definition before it will be created when you request it in @PostConstrust . getBeansWithAnnotation()应该找到一个Foo bean,如果它不是从它的 bean 定义中创建的,那么当您在@PostConstrust请求它时它将被创建。 You can try to force this scenario with @DependsOn however it may lead to circular dependency problem:您可以尝试使用@DependsOn强制这种情况,但它可能会导致循环依赖问题:

@Component
@DependsOn("testBean")
@CustomAnnotation
public class Foo {
}

@Service("testBean")
public class TestBean {

   @Autoware
   private Application context;

   @PostContruct
   public void init() {
      context.getBeansWithAnnotation(CustomAnnotation.class);
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我从PostConstruct注释的init方法得到警告 - I'm getting warning from PostConstruct annotated init method 显式调用@PostConstruct带注释的方法 - Call @PostConstruct annotated method explicitly 如何在 @Bean 注释方法或类似方法中创建多个 Spring bean? - How can I create multiple Spring beans in a @Bean-annotated method or anything similar? 我可以使用CDI在任何bean中查找所有带限定符的方法吗? - Can I use CDI to look for all qualifier-annotated methods in any beans? Spring:为什么用@PostConstruct 注释的方法不能是静态的? - Spring: Why method annotated with @PostConstruct cannot be static? 我可以在接口方法上使用@PostConstruct吗? - Can I use @PostConstruct on an interface method? 我可以获取方法调用的带注释参数的值吗? - Can I get the value of an annotated parameter of a method call? 如何在会话中获取所有初始化的托管bean? - How can I get all the initialized managed beans in the session? 注解方法上的@PostConstruct无法在springboot应用程序启动时启动 - @PostConstruct annotated on method doesnt start up on springboot application start up 使用Mockito模拟时无法调用以@PostConstruct注释的方法 - Unable to invoke method annotated with @PostConstruct when mocked using Mockito
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM