If you want a bean to appear under certain conditions:
- The application's classpath contains a specific Custay created
- Only when a particular bean is declared will it be created
- A specific environment variable is set before a bean is created
Prior to spring 4, it was difficult to achieve this level of conditional configuration, but Spring4 introduced a new @conditional annotation that could be used with @bean annotations. If the given condition evaluates to true, the bean will be created, otherwise the bean will be ignored.
Example: Setting the Magic Environment property to instantiate Magicbean
1 @Bean 2 @Conditional (magicexistscondition. Class) // conditionally Create Bean3 public Magicbean Magicbean () {4 returnnew Magicbean (); 5 }
A class is given in the @conditional here, which indicates the condition, that is, magiccondition.
The condition interface is as follows:
1 Public Interface Condition {2 Boolean matches (Conditioncontext ctxt, annotatedtypemetadata metadata); 3 }
Classes that are set to @conditional can make arbitrary implementations of the type of the condition interface. Thus, only the implementation of the matches () method needs to be provided. If the matches () method returns True, a bean with @conditional annotations is created. Conversely, these beans are not created.
Implementation of Magicexistscondition:
1 Public class Implements Condition {2 Public Boolean matches (conditioncontext context, annotatedtypemetadata metadata) {3 Environment env = context.getenvironment (); 4 return env.containsproperty ("Magic"); // Check if there is a magic attribute 5 }6 }
The above example only gets the content of Environment,conditioncontext is quite rich, it is an interface:
1 Public Interface Conditioncontext {2 beandefinitionregistry getregistry (); 3 configurablelistablebeanfactory getbeanfactory (); 4 Environment getenvironment (); 5 Resourceloader Getresourceloader (); 6 ClassLoader getClassLoader (); 7 }
Through Conditioncontext, you can get the following resources:
- Checks the bean definition with the beandefinitionregistry returned by GetRegistry ()
- Check if the bean exists (this dick) and even probe the Bean's properties with the Configurablelistablebeanfactory returned by Getbeanfactory ()
- Getenvironment () returns the environment check if the environment variable exists what its value is
- Reads and probes the resources loaded by the Resourceloader returned by Getresourceloader ()
- Load and check the existence of a class with the ClassLoader returned by getClassLoader ()
Annotationedtypemetadata allows us to examine what other annotations are on the method with @bean annotations.
1 Public InterfaceAnnotatedtypemetadata {2 Booleanisannotated (String var1);3Map<string, object>getannotationattributes (String var1);4Map<string, object> getannotationattributes (String var1,Booleanvar2);5Multivaluemap<string, object>getallannotationattributes (String var1);6Multivaluemap<string, object> getallannotationattributes (String var1,Booleanvar2);7}
With the Isannotated method, we can determine if there are other specific annotations to the method with @bean annotations. Other methods enable you to examine the properties of other annotations on the method of the @bean annotation.
Beginning with spring 4, @Profile annotations were refactored to be based on @conditional and condition implementations.
The implementation of @profile in spring 4 is as follows:
1 @Retention (retentionpolicy.runtime) 2 @Target ({elementtype.type, elementtype.method}) 3 @Documented 4 @Conditional ({profilecondition. Class})5 public @Interface profile {6 string[] Value (); 7 }
Profilecondition:
1 classProfileconditionImplementsCondition {2 @Override3 Public Booleanmatches (conditioncontext context, Annotatedtypemetadata metadata) {4 if(Context.getenvironment ()! =NULL) {5multivaluemap<string, object> attrs = metadata.getallannotationattributes (profile.class. GetName ());6 if(Attrs! =NULL) {7 for(Object value:attrs.get ("value"))) {8 if(Context.getenvironment (). Acceptsprofiles (((string[]) value )) {9 return true;Ten } One } A return false; - } - } the return true; - } -}
Profilecondition All properties for @profile annotations are obtained through Annotatedtypemetadata. With this information, it explicitly checks the Value property, which contains the bean's profile name. The profile is then checked (with the Acceptsprofiles () method) based on the environment obtained by Conditioncontext and is active.
Summarize
- @Conditional annotations can implement a bean's conditional declaration
- Profile is refactored using @conditional in spring 4
- There are two key interfaces in the implementation of the conditional configuration: Conditioncontext and Annotatedtypemetadata, which play a key role in detecting conditions
- There is no telling how to implement conditional configuration in XML
- It's pretty cool to find a config in Java here.
Spring Advanced Assembly (ii) conditionally-typed beans