Does annotating a bean @DependsOn mean that the dependent bean will be instantiated or initialized?


User130532:

I am using Spring 3.0.2. I have two relatively simple bean definitions. One has a @PostConstruct(bean 'A') which fires a series of events that need to prepare the @DependsOn bean(bean 'B'). However, even though I said bean 'A' depends on bean 'B', it seems that bean 'A''s events (lifecycle methods) are run before bean 'B' is fully initialized.

Does declaring a bean to be "dependent" (or, for that matter, dependent on the bean definition) via @DependsOn means that the lifecycle methods of that dependent bean will complete before the bean that depends on it?

Will the lifecycle methods of Bean 'B' complete before Bean 'A'?

renew

Bean A is a custom class that uses a JMS template to send a message announcing that it has been initialized.

The receiver of said message processes it and forwards its configuration to the MessageListeningContainer (Bean B).

The first part happens before Bean B is started by DefaultLifecycleProcessor.

@Component
@DependsOn("beanB")
public class BeanA {
    @PostConstruct
    public void init() {
        // do stuff
    }
}

<bean id="beanB" class="org.springframework.jms.listener.DefaultMessageListenerContainr">
    <!-- other configuration -->
</bean>

I added the injection of bean b in the init method along with two logging statements:

container.isRunning();
container.isActive();

I looked at the spring source and after the initialization method (doInitialized is done), isActive is set to true. After doStart completes, isRunning is set. doStart is triggered by DefaultLifecycleProcessor, which happens after the @PostConstruct annotated method is called.

How can I guarantee that my Postconstruct method is called AFTER bean b, and has been initialized and started?

mrembisz :

In your particular case @PostConstruct, bean A's method will not be called until B is fully initialized (i.e. B). All its dependencies are injected and @PostConstructexecution is done.

Update: Since you're relying on Spring Lifecycle functionality here, is there a way to implement it Lifecyclein A and move the JMS calls start()there?

Related


What does the "&" in the bean name mean?

mark I'm looking at a Spring Qualifierannotation like this: @Autowired @Qualifier("&fooStreamBuilder") private FooFactoryBean fooFactoryBean; I've never seen "&" in a comment like this before. What is it and what does it do? Gary Russell If the bean is a fact

What does "agent a bean" mean?

Cory Klein: At work and online, I keep hearing the term "agent" in relation to enterprise Java development. For example, metrics-spring uses the following phrases: This module does the following: Create metrics and proxy beans containing methods annotated with

What does the "&" in the bean name mean?

mark I'm looking at a Spring Qualifierannotation like this: @Autowired @Qualifier("&fooStreamBuilder") private FooFactoryBean fooFactoryBean; I've never seen "&" in a comment like this before. What is it and what does it do? Gary Russell If the bean is a fact

What does "agent a bean" mean?

Corey Klein At work and online, I often hear the term "agent" in relation to enterprise Java development. For example, metrics-spring uses the following phrases: This module does the following: Create metrics and proxy beans containing methods annotated with @

What does "agent a bean" mean?

Cory Klein: At work and online, I keep hearing the term "agent" in relation to enterprise Java development. For example, metrics-spring uses the following phrases: This module does the following: Create metrics and proxy beans containing methods annotated with

What does the "&" in the bean name mean?

mark I'm looking at a Spring Qualifierannotation like this: @Autowired @Qualifier("&fooStreamBuilder") private FooFactoryBean fooFactoryBean; I've never seen "&" in a comment like this before. What is it and what does it do? Gary Russell If the bean is a fact

Session scoped bean not instantiated

pkalinow I want to create a session scoped bean to monitor HTTP session activation and passivation. Beans are very simple: package my.log; import javax.servlet.http.HttpSessionActivationListener; import javax.servlet.http.HttpSessionEvent; import org.apache.

Automatically instantiated bean parameters?

Dero I am new to Spring and Spring MVC I'm taking a course where they introduce the following @Configuration classes: @Configuration public class MailConfig { @Bean @ConditionalOnProperty(name="spring.mail.host", havingValue="foo", mat

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

benstpierre: I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wit

Hibernate/JPA - Annotating bean methods vs fields

Benstpierre : I have a simple question about Hibernate usage. I keep seeing people using JPA annotations in one of two ways, by annotating the fields of the class and also by annotating the get method of the corresponding bean. My question is the following: wi

Is @DependsOn required for another injected CDI bean?

Jens Given two beans annotated with @Startup: @Singleton @Startup @DependsOn("B") public A { @Inject private B b; } @Singleton @Startup public B {} Is @DependsOn required in this case to ensure that B is initialized before A? Or is there a conven

Is @DependsOn required for another injected CDI bean?

Jens Given two beans annotated with @Startup: @Singleton @Startup @DependsOn("B") public A { @Inject private B b; } @Singleton @Startup public B {} Is @DependsOn required in this case to ensure that B is initialized before A? Or is there a conven

How to @SpringBootTest() a class @DependsOn() another bean

Andre Risnes I have a Spring Boot application with a class @DependsOn another bean like this: @Configuration public class SomeBeanConfig { @Bean public SomeClass someBean() { ... @DependsOn("someBean") public class ClassIWantToTest { ... @S

Spring check if a lazy bean is instantiated

Eugen Covaci: Suppose I have a lazy Spring managed bean in a MyBeancustom scope like this : @Configuration public class MyConfiguration { @Scope("custom") @Lazy @Bean MyBean myBean () { return new MyBean(); } } and another Spring

@PostConstruct of Bean instantiated class is not called

Sandeepan Nath: 我正在探索这个项目https://github.com/davidmarquis/redis-scheduler。我已经在本地设置好了,并且能够运行集成测试用例。现在,我试图创建一个应用程序端点来设置一些延迟的任务并启动轮询线程。我已经能够将任务存储在redis zset中(通过RedisTaskScheduler-> scheduleAt()方法),但是我无法启动轮询过程,该过程应该从zset中删除密钥。 以下是我所做的- 以下是我创建的入口点- package com.githu

JSF managed bean in jar not instantiated

Mahendra I have created two jsf projects in JSF. One of them is the base project with a single session bean. The base project is packaged into a .jar file (with a /META-INF/faces-config.xml file) and included in another project (clientproj). The problem is tha

@PostConstruct of Bean instantiated class is not called

Sandeepan Nath: I am exploring this project https://github.com/davidmarquis/redis-scheduler . I have it set up locally and am able to run the integration test cases. Now, I'm trying to create an application endpoint to setup some delayed tasks and start a poll

JSF managed bean in jar not instantiated

Mahendra I have created two jsf projects in JSF. One of them is the base project with a single session bean. The base project is packaged into a .jar file (with a /META-INF/faces-config.xml file) and included in another project (clientproj). The problem is tha