First, the condition annotation @conditional
In the previous study, through the activity profile, we can get different beans. Spring4 provides a more general creation of conditional-based beans that use @conditional annotations.
@Conditional create a specific bean based on satisfying a particular condition. For example, when a jar is wrapped under a classpath, one or more beans are automatically configured, or only one bean is created to create another bean. In general, the creation behavior of the bean is controlled according to certain conditions, so that we can use this feature for some automatic configuration.
The following example will be conditional on a different operating system by implementing the condition interface and overriding its matches method to construct the judging condition. If you run the program under Windows, the Output List command is dir; if you run the program under a Linux system, the output List command is LS.
Example:
1. Definition of conditions of judgment
(1) Criteria for determining windows
Packagecom.ecworking.condition;Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata; Public classWindowsconditionImplementsCondition {@Override Public Booleanmatches (Conditioncontext Conditioncontext, Annotatedtypemetadata annotatedtypemetadata) {returnConditioncontext.getenvironment (). GetProperty ("Os.name"). Contains ("Windows"); }}
(2) Criteria for determining Linux
Packagecom.ecworking.condition;Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata; Public classLinuxconditionImplementsCondition {@Override Public Booleanmatches (Conditioncontext Conditioncontext, Annotatedtypemetadata annotatedtypemetadata) {returnConditioncontext.getenvironment (). GetProperty ("Os.name"). Contains ("Linux"); }}
(3) Criteria for determining Mac
Packagecom.ecworking.condition;Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata; Public classMacconditionImplementscondition{@Override Public Booleanmatches (Conditioncontext Conditioncontext, Annotatedtypemetadata annotatedtypemetadata) {returnConditioncontext.getenvironment (). GetProperty ("Os.name"). Contains ("Mac"); }}
2. Bean classes under different systems
(1) interface
Package com.ecworking.condition; Public Interface Listservice { String showlistcmd ();}
(2) The Bean class to be created under Windows
Package com.ecworking.condition; Public class Implements listservice{ @Override public String showlistcmd () { return "dir"; }}
(3) The Bean class to be created under Linux
Package com.ecworking.condition; Public class Implements listservice{ @Override public String showlistcmd () { return "ls "; }}
(4) The Bean class to be created under Mac
Package com.ecworking.condition; Public class Implements listservice{ @Override public String showlistcmd () { return "ls "; }}
3. Configuration Classes
Packagecom.ecworking.condition;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Conditional;Importorg.springframework.context.annotation.Configuration; @Configuration Public classconditionconfig {@Bean @Conditional (windowscondition.class)//by @conditional annotations, the compliance with Windows conditions is instantiated Windowslistservice PublicListservice Windowslistservice () {return NewWindowslistservice (); } @Bean @Conditional (linuxcondition.class)//by @conditional annotations, the Linux-compliant condition is instantiated Linuxlistservice PublicListservice Linuxlistservice () {return NewLinuxlistservice (); } @Bean @Conditional (maccondition.class)//by @conditional annotations, the compliant Mac condition is instantiated Maclistservice PublicListservice Maclistservice () {return NewMaclistservice (); }}
4. Running
Packagecom.ecworking.condition;ImportOrg.springframework.context.annotation.AnnotationConfigApplicationContext; Public classMain { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext context=NewAnnotationconfigapplicationcontext (Conditionconfig.class); Listservice Listservice= Context.getbean (Listservice.class); System.out.println (Context.getenvironment (). GetProperty ("Os.name") + "List command under System is:" +listservice.showlistcmd ()); }}
Operation Result:
Only the Windows and Linux instances in the book were used for the first run, and the results were run listservice listservice = Context.getbean (Listservice.class); The times is wrong because there are no eligible instances created because I used a Mac that used to think that Mac was a Linux system and found that Mac OS is Mac, so add maccondition and maclistservice to work.
Spring Boot Combat Notes (eight)--Spring Advanced topic (conditional annotation @conditional)