springboot中@Conditional注解如何使用
这篇文章给大家介绍springboot 中@Conditional注解如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
专注于为中小企业提供成都做网站、网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业万安免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
启动类
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注解如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
文章名称:springboot中@Conditional注解如何使用
网页网址:http://azwzsj.com/article/gsoddo.html