In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.