Java – spring: how to initialize bean a after bean B is loaded?

I have a beana whose constructor requires beanb

Is there a way to do this?

Another way is to call BeanB. after all bean in the context is created. init().

Cheers!

Solution

You can use ApplicationContext Init method is used in XML to specify init method If you want the bean to be instantiated after another, you can use dependencies on, even though any ref element (constructor args in this example) will implicitly place dependencies

This will first initialize bean B using the init method and use it as the constructor parameter of a when it is completed

<!-- Bean B -->
<bean id="beanB" 
    class="classB"
    init-method="init"
/>

<!-- Bean A -->
<bean id="beanA" 
    class="classA"
    init-method="anotherInit">
        <constructor-arg ref="beanB"/>
    </bean>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>