Spring Conditional annotation @ Conditional

Keywords: Linux Windows Java Junit

Spring 4 provides a more general way to create condition based beans, using @ Conditional annotations.

The following example will take different operating systems as conditions, and we will construct judgment conditions by implementing the Condition interface and overriding its matches method. For example, run the program under Windows to output dir, and output ls under Linux.

1, Bean s in different systems

1. Declare interface

package cn.wuweixiang.springbootdemo.service;

/**
 * ListService
 *
 * @author weixiang.wu
 * @date 2018 -08-22 15:39
 */
public interface ListService {
	/**
	 * Show list cmd string.
	 *
	 * @return the string
	 */
	String showListCmd();
}

2. Bean to be created under Windows

package cn.wuweixiang.springbootdemo.service.impl;

import cn.wuweixiang.springbootdemo.service.ListService;

/**
 * WindowsListServiceImpl
 *
 * @author weixiang.wu
 * @date 2018-08-22 15:40
 **/
public class WindowsListServiceImpl implements ListService {
	@Override
	public String showListCmd() {
		return "dir";
	}
}

3. Bean to be created under Linux

package cn.wuweixiang.springbootdemo.service.impl;

import cn.wuweixiang.springbootdemo.service.ListService;

/**
 * LinuxListServiceImpl
 *
 * @author weixiang.wu
 * @date 2018-08-22 15:42
 **/
public class LinuxListServiceImpl implements ListService {
	@Override
	public String showListCmd() {
		return "ll";
	}
}

2, Definition of judgment conditions

package cn.wuweixiang.springbootdemo.condition;

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

import java.util.Objects;

/**
 * ConditionalConfig
 *
 * @author weixiang.wu
 * @date 2018-08-22 15:55
 **/
public class LinuxCondition implements Condition {


	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		return Objects.requireNonNull(context.getEnvironment().getProperty("os.name")).contains("Linux");
	}
}
package cn.wuweixiang.springbootdemo.condition;

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

import java.util.Objects;

/**
 * ConditionalConfig
 *
 * @author weixiang.wu
 * @date 2018-08-22 15:55
 **/
public class WindowsCondition implements Condition {


	@Override
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		return Objects.requireNonNull(context.getEnvironment().getProperty("os.name")).contains("Windows");
	}
}

3, Configuration class

package cn.wuweixiang.springbootdemo.config;

import cn.wuweixiang.springbootdemo.condition.LinuxCondition;
import cn.wuweixiang.springbootdemo.condition.WindowsCondition;
import cn.wuweixiang.springbootdemo.service.ListService;
import cn.wuweixiang.springbootdemo.service.impl.LinuxListServiceImpl;
import cn.wuweixiang.springbootdemo.service.impl.WindowsListServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;

/**
 * ConditionalConfig
 *
 * @author weixiang.wu
 * @date 2018 -08-22 15:55
 */
public class ConditionalConfig {

	/**
	 * Use @ Conditional annotation to instantiate LinuxListService if it meets Linux conditions
	 *
	 * @return the list service
	 */
	@Bean
	@Conditional(LinuxCondition.class)
	public ListService linuxListService() {
		return new LinuxListServiceImpl();
	}

	/**
	 * Use @ Conditional annotation to instantiate WindowsListService if it meets Windows conditions
	 *
	 * @return the list service
	 */
	@Bean
	@Conditional(WindowsCondition.class)
	public ListService windowsListService() {
		return new WindowsListServiceImpl();
	}

}

4, Test class

package cn.wuweixiang.springbootdemo;

import cn.wuweixiang.springbootdemo.config.ConditionalConfig;
import cn.wuweixiang.springbootdemo.service.ListService;
import org.junit.After;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * SpringConditionalTest
 *
 * @author weixiang.wu
 * @date 2018-08-22 16:00
 **/
public class SpringConditionalTest {

	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionalConfig.class);

	@Test
	public void contextTest() {
		ListService listService = context.getBean(ListService.class);

		System.out.println(context.getEnvironment().getProperty("os.name")
				+ "The list command under the system is:"
				+ listService.showListCmd());
	}

	@After
	public void closeContext() {
		context.close();
	}

}

Posted by aesthetics1 on Sun, 05 Jan 2020 06:28:13 -0800