7.8.6: Force bean Initialization

Source: Internet
Author: User

Spring has a default rule:Always initialize the main bean and then initialize the dependent bean.

In most cases, dependencies between beans are very direct. Spring containers inject bean dependencies before returning bean instances. If bean a depends on bean B, the spring container will automatically initialize bean B when the program requests bean A, and then inject bean B into Bean, finally, bean a with complete dependencies is returned to the program.

In extreme cases, dependencies between beans are not direct enough. For example, if another bean is used in the initialization block of a class, Spring always initializes the main Bean first. When the initialization block is executed, the main bean is not instantiated, and the dependent bean is not instantiated yet. In this case, an exception is thrown.

You can useDepends-on
Attribute, which can be forced to initialize one or more beans before initializing the main bean.

<Bean id = "beanone" class = "examplebean" depends-on = "manager"> <property name = "manager" ref = "manager"/> </bean> <Bean id = "manager" class = "managerbean"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.