栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 根据不同操作系统创建不同 Bean

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 根据不同操作系统创建不同 Bean

LIXI.FUN - SpringBoot 根据不同操作系统创建不同 Bean
在给小伙伴解决一些问题的时候,遇到的这个问题,在 jna 中,windows 和 linux 需要加载的动态链接库的路径不一样。

于是乎,配置文件修改为:

dynamic-lib:
  windows:
    xxx-path: D:developxxx
    yyy-path: D:developyyy
  linux:
    xxx-path: /usr/lib/xxx
    yyy-path: /usr/lib/yyy

DynamicLibConfigBase.java

import lombok.Data;


@Data
public class DynamicLibConfigBase {
    private String xxxPath;
    private String yyyPath;
}

LinuxDynamicLibConfig.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;


@Configuration
@Conditional(LinuxCondition.class) // 这里决定了只有在 Linux 中才会创建这个 Bean
@ConfigurationProperties(prefix = "dynamic-lib.linux")
public class LinuxDynamicLibConfig extends DynamicLibConfigBase {
}

WindowsDynamicLibConfig.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;


@Configuration
@Conditional(WindowsCondition.class)  // 这里决定了只有在 Windows 中才会创建这个 Bean
@ConfigurationProperties(prefix = "dynamic-lib.windows")
public class WindowsDynamicLibConfig extends DynamicLibConfigBase {
}

WindowsConditon.java

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 context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").toLowerCase().contains("windows");
    }
}

LinuxCondition.java

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 context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").toLowerCase().contains("linux");
    }
}

在调用的时候,只要注入 DynamicLibConfigBase 类型的 Bean 就可以直接使用了。

class DynamicLibInterfaceTest {

    @Autowired
    private DynamicLibConfigBase dynamicConfig;

    @Test
    void getXxxPath() {
        System.out.println(dynamicConfig.getXxxPath());
    }

    @Test
    void getYyyPath() {
        System.out.println(dynamicConfig.getYyyPath());
    }
}

在不同的操作系统下就会输出相对应的动态链接库的路径,在新增 动态链接库 的时候,只需要在配置文件中加入在 windows 和 linux 中的不同路径,同时在 DynamicLibConfigBase 中增加对应 Field 就可以了。

总结

  • @Condition 决定在什么操作系统下创建哪个 Bean;
  • 使用时注入基类即可。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1007541.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号