Spring Combat (v) conditionally creating beans in spring

Source: Internet
Author: User

1. @Conditional set conditions for the generated bean

Spring 4 introduces a new annotation [email protected], which is used in @bean methods.

If the given condition evaluates to true,spring, the bean is created and, if false, ignored.

@Bean @conditional (userexistscondition. class  public User User () {  returnnew  User ();  }

Here, a conditional class is given in the @Conditional, which is the condition that the bean is created to satisfy.

This condition class can be any type that implements the condition interface (just implement the Boolean matches () method).

2, the function of the Conditioncontext interface (an important interface to use)

    • Check the bean definition with the beandefinitionregistry returned by GetRegistry ();
    • Check that the bean exists, or even probe the bean properties, with the Configurablelistablebeanfactory returned by Getbeanfactory ().
    • The environment returned with Getenvironment () checks to see if the environment variable exists and what its value is;
    • Reads and probes the resources loaded by the Resourceloader returned by Getresourceloader ();
    • Load and check the existence of the class with the ClassLoader returned by getClassLoader ();

  

3. Annotatedtypemetadata interface

What other annotations are used to examine the method with @bean annotations, that is, to determine if there are any.

4, Spring 4, @Profile annotations are reconstructed, making them based on @conditional and condition implementation

Spring Combat (v) conditionally creating beans in spring

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.