How to use the @ Conditional annotation in springboot
This article introduces how to use the @ Conditional annotation in springboot. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Startup class
Package com.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Hello world! * * / @ SpringBootApplicationpublic class App {public static void main (String [] args) {SpringApplication.run (App.class,args);}}
1. Implement the condition interface
Package com.demo.condition;import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext;import org.springframework.core.type.AnnotatedTypeMetadata;public class WindowsCondition implements Condition {@ Override public boolean matches (ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {String property = conditionContext.getEnvironment () .getProperty ("os.name"); return property.contains ("Window");}} package com.demo.condition;import org.springframework.context.annotation.Condition;import org.springframework.context.annotation.ConditionContext Import org.springframework.core.type.AnnotatedTypeMetadata;public class LinuxCondition implements Condition {@ Override public boolean matches (ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {String property = conditionContext.getEnvironment () .getProperty ("os.name"); return property.contains ("Linux");}}
Configure bean object
Package com.demo.config;import com.demo.condition.LinuxCondition;import com.demo.condition.WindowsCondition;import com.demo.serviceI.DemoService;import com.demo.serviceImp.DemoServiceLinuxImp;import com.demo.serviceImp.DemoServiceWindowImp;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Conditional;import org.springframework.context.annotation.Configuration;@Configurationpublic class OSconfig {@ Bean @ Conditional (LinuxCondition.class) public DemoService linuxCondition () {return new DemoServiceLinuxImp () @ Bean @ Conditional (WindowsCondition.class) public DemoService winCondition () {return new DemoServiceWindowImp ();}}
Service interface
Package com.demo.serviceI;public interface DemoService {String info ();}
Service interface implementation
Package com.demo.serviceImp;import com.demo.serviceI.DemoService;public class DemoServiceLinuxImp implements DemoService {@ Override public String info () {return "linux";}} package com.demo.serviceImp;import com.demo.serviceI.DemoService;public class DemoServiceWindowImp implements DemoService {@ Override public String info () {return "window";}}
Control layer injection by type
Package com.demo.action;import com.demo.serviceI.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class DemoAction {@ Autowired private DemoService demoService @ RequestMapping (value = "health.json") public String healt () {return "{\" status\ ":\" UP\ ",\" diskSpace\ ": {\" status\ ":\" UP\ ",\" total\ ": 249769230336,\" free\ ": 71914618880,\" threshold\ ": 10485760},\" db\ ": {\" status\ ":\" UP\ ",\" database\ ":\" MySQL\ ",\" hello\ ": 1}" @ RequestMapping (value = "user/info") public String info () {return demoService.info ();}}
Access interface
This is enough about how to use the @ Conditional annotation in springboot. I hope the above can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.