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