这篇文章给大家介绍springboot 中@Conditional注解如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
启动类
package com.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Hello world! * */ @SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class,args); } }
1.实现condition接口
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"); } }
配置bean对象
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; @Configuration public class OSconfig { @Bean @Conditional(LinuxCondition.class) public DemoService linuxCondition(){ return new DemoServiceLinuxImp(); } @Bean @Conditional(WindowsCondition.class) public DemoService winCondition(){ return new DemoServiceWindowImp(); } }
服务接口
package com.demo.serviceI; public interface DemoService { String info(); }
服务接口实现
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"; } }
控制层按类型注入
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; @RestController public 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(); } }
访问接口
关于springboot 中@Conditional注解如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。