In the last blog we talked about the "Java EE development of Spring in the multi-threaded programming and task timer detailed ", this blog we will talk about the conditional annotation @Conditional and the combination of conditions. Conditional annotations say the simple point is to select the creation of a Bean object based on a specific condition. Conditional annotations are different things that can be done according to different conditions. In Spring , conditional annotations can be described as an embodiment of state patterns in design patterns , and are also part of the application of polymorphism in object-oriented programming. Combining annotations is the combination of existing annotations. Specific introductions and examples are given below.
I. condition annotations [EMAIL protected]
In this section of this blog, let's talk about conditional annotations, as the name implies, conditional annotations can be based on different conditions to make different things. In spring, conditional annotations can be described as an embodiment of state patterns in design patterns, and are also part of the application of polymorphism in object-oriented programming.
In the Spring Framework, when we use conditional annotations, we create a class for each individual condition, and we choose different tasks to execute according to the conditions in which the class corresponds. Of course, when declaring a task, we typically use an interface to declare it. Because we specify specific classes under specific conditions in the spring configuration class. Next, let's look at how @Conditional annotations are used in the spring framework.
Of course, the same Service interface is mutually exclusive in the set of conditions, meaning that only one condition is true in a particular case.
1, create the service interface and the specific service class
Let's start by creating a service interface, and then create two service classes based on the situation that follows that interface. Below we will specify different service objects under different conditions in the configuration class. First, let's create the interface for the service. The code below is the interface of the service we created, which is simpler and has only one description method. In the specific service class we will give a concrete implementation of the description () method, using this method to distinguish the implementation of different classes.
Package com.zeluli.conditional; Public Interface Conditinalserviceinteface { public String description ();}
After Serviceinterface is created, we should create a specific class. The firstconditionservice and secondconditionservice two classes below are implemented Conditinalserviceinteface interface, and gives a concrete implementation of the description () method. Later, we will give the appropriate conditions when the Bean is configured in the class below. This section is only part of the preparation.
Packagecom.zeluli.conditional; Public classFirstconditionserviceImplementsConditinalserviceinteface { PublicString Description () {return"First service with a set of conditions"; }}========================================= Packagecom.zeluli.conditional; Public classSecondconditionserviceImplementsConditinalserviceinteface { PublicString Description () {return"Second Conditional service"; }}
2. Create @conditional corresponding conditional class
After creating the service interface and the service class, we'll create the conditional classes required to @Conditional annotations. Each conditional class corresponds to a separate situation in which the conditional class in spring needs to implement the condition interface. Below are the two conditional classes we created.
Both of these conditional classes implement the Conditionin the spring framework, and the implementation of the matches () method is given. The return value of the matches () method is a Boolean value that, if returned false, indicates that the condition of the conditional class is not true, and that the condition corresponding to the condition is valid if it is returned. To simplify the operation, we specify that the firstconditional corresponding condition is false, and that the secondconditional corresponds to True. The implementation of the specific condition class is shown below.
Packagecom.zeluli.conditional;Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata; Public classFirstconditionalImplementsCondition {//methods of providing conditions Public Booleanmatches (conditioncontext context, Annotatedtypemetadata metadata) {return false; }}========================================== Packagecom.zeluli.conditional;Importorg.springframework.context.annotation.Condition;ImportOrg.springframework.context.annotation.ConditionContext;ImportOrg.springframework.core.type.AnnotatedTypeMetadata; Public classSecondconditionalImplementsCondition { Public Booleanmatches (conditioncontext context, Annotatedtypemetadata metadata) {return true; }}
3. Conditional configuration In the Java configuration class
After the service interface, the service class, and the corresponding conditions are created, we then associate the conditional class with the service class object in the Java configuration class. The code snippet below is the corresponding configuration class for that section. When declaring a bean of the Firstconditionservice class, we use @Conditional annotations,@Conditional parameters are Firstconditional.class, which means that firstconditionservice objects are instantiated when the firstconditional class corresponds to a condition.
In the same vein, the secondconditionservice corresponding to the underlying condition is secondconditional. The specific code is shown below.
Packagecom.zeluli.conditional;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Conditional;Importorg.springframework.context.annotation.Configuration, @Configuration @componentscan ("Com.zeluli.conditional") Public classconditionalconfig {@Bean @Conditional (firstconditional.class)//specifying a condition class PublicFirstconditionservice Getfirstconditionalservice () {return NewFirstconditionservice (); } @Bean @Conditional (secondconditional.class) PublicSecondconditionservice Getsecondconditionservice () {return NewSecondconditionservice (); }}
4. Create the Main method to test
It's time to test again, below we get the context from the conditionalconfig configuration class above and get the corresponding service object from the context. When we get the object, we use the conditionalserviceinterface interface to get and declare the bean. That is, the service variable hosts an object that is a class in all classes that implement the Conditionalserviceinterface interface. When the conditions for a service class are true, the objects of that class are created.
As we have mentioned above, the conditions corresponding to each class in theconditionalserviceinterface interface must be mutually exclusive, that is, only one of these conditions is true under certain circumstances. Because we return true for the second condition, the condition is set, and then the object of the Secondconditionalservice class is called. So when we call the service's description () method, the corresponding method in the Secondconditionalservice class is called. The details are as follows.
Second, the combination of annotations
Combination annotations This is a good understanding, is to combine multiple annotations into a piece to create a new annotation. Using this new annotation is equivalent to using all the annotations in the combined annotation. This feature is pretty useful, so let's look at how to create and use composite annotations.
1. Creation of combined annotations
Next we'll look at a simple example of how to combine multiple annotations into one piece. In the previous spring configuration class, we used the annotations @Configuration and @ComponentScan frequently, and then we packaged them together to form a new annotation.
The combinationconfiguration annotation Below is a new annotation of our composition, which is decorated with @Configuration and @componentscan , which means @ Combinationconfiguration annotations also feature both the @Configuration and @componentscan annotations.
Packagecom.zeluli.combination.annotation;Importjava.lang.annotation.Documented;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration, @Target (elementtype.type) @Retention ( Retentionpolicy.runtime) @Documented @configuration@componentscan Public@Interfacecombinationconfiguration {string[] value ()default{};}
2. Use of combined annotations
After creating the corresponding combination of annotations, it is time to use, the above annotations are used and the general annotation is no different. Just this annotation represents the two annotations that were previously written @configuration and @componentscan. The code below is how the combined annotations are used.
OK, today's blog is here first, GitHub source share address:https://github.com/lizelu/SpringDemo
conditional annotations, combination annotations and meta-annotations in the spring of Java EE Development