千家信息网

springboot 中@Conditional注解如何使用

发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,这篇文章给大家介绍springboot 中@Conditional注解如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。启动类package com.demo;import
千家信息网最后更新 2025年02月01日springboot 中@Conditional注解如何使用

这篇文章给大家介绍springboot 中@Conditional注解如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

启动类

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.实现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;@Configurationpublic 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;@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();    }}

访问接口

关于springboot 中@Conditional注解如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0