A dot eyeball.

In Spring4.x common configuration (4):Spring Profile, we know that different beans can be obtained through active profiles. Spring4 provides a more generic creation of conditional-based beans using the @conditional annotation.

@Conditional creates a specific Bean that satisfies a specific condition. For example, one or more beans are automatically configured when a jar package is in a classpath; Or another Bean will be created only if one Bean is created. Basically, we control the Bean creation behavior based on specific conditions so that we can take advantage of this feature for some automatic configuration.

Conditional annotations are used a lot in Spring Boot, and we’ll talk about that when we get a chance.

The following example, which will be conditional on a different operating system, will construct the judgment Condition by implementing the Condition interface and overriding its matches method. If you run the program in Windows, the output list command is dir. If you run the program in Linux, the command output list is ls.

Example 2.

1. Judge the definition of conditions

1.1 Determine the conditions of Windows

package org.light4j.sping4.senior.conditional; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; public class WindowsCondition implements Condition { public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Windows"); }}Copy the code

1.2 Determine the conditions of Linux

package org.light4j.sping4.senior.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition implements Condition {

    public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Linux");
    }
}Copy the code

2. Bean classes under different systems

2.1 interface

package org.light4j.sping4.senior.conditional;

public interface ListService {
    public String showListCmd();
}Copy the code

2.2 Bean classes to be created in Windows

package org.light4j.sping4.senior.conditional; public class WindowsListService implements ListService { @Override public String showListCmd() { return "dir"; }}Copy the code

2.3 The Bean class to be created under Linux

package org.light4j.sping4.senior.conditional; public class LinuxListService implements ListService{ @Override public String showListCmd() { return "ls"; }}Copy the code

3. The configuration class

package org.light4j.sping4.senior.conditional; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @ Configuration public class ConditionConifg {@ Bean @ Conditional (WindowsCondition. Class) / / (1) public ListService windowsListService() { return new WindowsListService(); } @bean@conditional (linuxcondition.class) //② public ListService linuxListService() {return new linuxListService(); }}Copy the code

Code explanation:

① Instantiate windowsListService if Windows conditions are met by @Conditional annotation. ② If Linux conditions are met, instantiate linuxListService via @Conditional annotation.

4. Run

package org.light4j.sping4.senior.conditional; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConifg.class); ListService listService = context.getBean(ListService.class); System.out.println(context.getenvironment ().getProperty("os.name") + "+ listService.showlistcmd ()); context.close(); }}Copy the code

My system is Windows, and the running result is as follows:

5. Source code examples:

Github Address: Click to view code cloud address: click to view

exceptional
Welcome to follow the life designer’s wechat public account



longjiazuoA