Spring Boot: Conditional annotations @condition

Source: Internet
Author: User

@Conditional creates a specific bean based on satisfying a particular condition (the creation of a condition-based bean, i.e., the use of @conditional annotations).

For example, when a jar is wrapped in the path of a class, it automatically configures one or more beans, or only one bean is created to create another bean.

By implementing the condition interface and rewriting the period matches method to construct the judging condition. If the Windows system washes the program, then the output List command dir, if running the program under the Linux operating system, the output command is: LS.

1. Definition of conditions of judgment

Determine the conditions for Windows

1 Package Ch2.conditional2;2 3 //conditional annotations, and to replicate such4 import org.springframework.context.annotation.Condition;5 //Conditional Annotations Container6 import Org.springframework.context.annotation.ConditionContext;7 import Org.springframework.core.type.AnnotatedTypeMetadata;8 9  Public classLinuxcondition implements Condition {Ten  One @Override A      PublicBoolean matches (Conditioncontext context, Annotatedtypemetadata metadata) { -         //TODO auto-generated Method Stub -         returnContext.getenvironment (). GetProperty ("Os.name"). Contains ("Linux"); the     } -  -}

Criteria for determining Linux

1 Package Ch2.conditional2;2 3 //conditional annotation Interface class, replication Metches4 import org.springframework.context.annotation.Condition;5 import Org.springframework.context.annotation.ConditionContext;6 import Org.springframework.core.type.AnnotatedTypeMetadata;7 8  Public classWindowscondition implements Condition {9 Ten @Override One      PublicBoolean matches (Conditioncontext context, Annotatedtypemetadata metadata) { A         //TODO auto-generated Method Stub -         returnContext.getenvironment (). GetProperty ("Os.name"). Contains ("Windows"); -     } the  -}

2 bean classes under different systems

1 interface

1 Package Ch2.conditional2; 2 3  Public Interface Listservice {45      Public String showlistcmd (); 6 }

2windows the Bean class to be created

1 Package Ch2.conditional2;2 3 import Ch2.conditional2.ListService;4 5  Public classWindowslistservice implements Listservice {6 7 @Override8      PublicString Showlistcmd () {9         //TODO auto-generated Method StubTen         return "dir"; One     } A  -}

3linux the Bean class to be created

1 Package Ch2.conditional2;2 3 import Ch2.conditional2.ListService;4 5  Public classLinuxlistservice implements Listservice {6 7 @Override8      PublicString Showlistcmd () {9         //TODO auto-generated Method StubTen         return "ls"; One     } A  -}

3 Configuration Classes

Package Ch2.conditional2;//Configuration class declarationimport org.springframework.context.annotation.Configuration;//Bean Declarationimport Org.springframework.context.annotation.Bean;//Condition Limitsimport org.springframework.context.annotation.Conditional;//declared as a configuration class@Configuration Public classConditionconfig {//annotation as Bean@Bean//Annotation Condition Judgment@Conditional (windowscondition.class)     PublicListservice Windowslistservice () {return NewWindowslistservice (); }        //annotation as Bean@Bean//Annotation Condition Judgment@Conditional (linuxcondition.class)     PublicListservice Linuxlistservice () {return NewLinuxlistservice (); }}

4 run

Package Ch2.conditional2;import Org.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") +"The system's command to view the directory is:"+listservice.showlistcmd ());    Context.close (); }}

Run Result: Windows 10 system's View Directory command is: Dir

Spring Boot: Conditional annotations @condition

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.