Spring表达式评估操作系统 [英] Spring expression to evaluate OS

查看:120
本文介绍了Spring表达式评估操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想评估OS(操作系统)系统属性以加载环境对应的配置文件.例如.如果OS评估为Windows,则将properties-win.xml加载;如果OS评估为Unix或Linux,则将加载properties-unix.xml.

I want to evaluate the OS (operating system) system property to load the environment corresponding configuration file. E.g. if OS evaluates to Windows, the properties-win.xml will be loaded or if OS evaluates to Unix or Linux, the properties-unix.xml will be loaded.

下面的文字效果很好

#{(systemProperties['os.name'].indexOf('nix') >= 0 or systemProperties['os.name'].indexOf('nux') >= 0 or systemProperties['os.name'].indexOf('aix') > 0 ) ? 'linux' : 
        ((systemProperties['os.name'].indexOf('Win') >= 0) ? 'windows' : 'Unknow OS')}

但是我不想每次评估systemProperties['os.name'],而是希望将其包含在变量中,然后匹配条件. 我看到了#this变量的用法( http://docs.spring.io/spring-framework/docs/3.0.6.RELEASE/spring-framework-reference/html/expressions.html sec 6.5.10.1),并尝试使下面的文字

But in place of evaluating the systemProperties['os.name'] each time, I want to have this in a variable and then wants to match the condition. I saw the #this variable usage (http://docs.spring.io/spring-framework/docs/3.0.6.RELEASE/spring-framework-reference/html/expressions.html sec 6.5.10.1) and tried to make the below spel

#{systemProperties['os.name'].?((#this.indexOf('nix') >= 0 or #this.indexOf('nux') >= 0 or #this.indexOf('aix') > 0 ) ? 'unix' : 
    (#this.indexOf('win') >= 0) ? 'windows' : 'Unknown')}

但是以某种方式,它提供了解析异常.

But somehow, it's giving parsing exception.

有人可以提出任何建议吗?

Does anyone can suggest anything?

推荐答案

这种方法怎么样?需要弹簧4.

How about this approach, a more elegant one, I would say. Requires Spring 4.

public class WindowsEnvironmentCondition implements Condition {
     public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) {
          return context.getEnvironment().getProperty("os.name").indexOf("Win") >= 0;
     }
}

public class LinuxEnvironmentCondition implements Condition {
     public boolean matches(ConditionContext context,AnnotatedTypeMetadata metadata) {
          return (context.getEnvironment().getProperty("os.name").indexOf("nux") >= 0 
                 || context.getEnvironment().getProperty("os.name").indexOf("aix") >= 0);
     }
}

然后,您可以使用条件来注释所需的要加载的方法或类,具体取决于环境:

And then you can use the Conditions above to annotate the desired methods or classes to be loaded depending on the environment:

@Configuration
@Conditional(LinuxEnvironmentCondition.class)
@ImportResource("classpath:/META-INF/spring/linux.xml")
public class LinuxConfig {

private @Value("${test}") String message;

public @Bean
ExampleService service() {
    ExampleService service = new ExampleService();
    service.setMessage(message);
    return service;
}
}

@Configuration
@Conditional(WindowsEnvironmentCondition.class)
@ImportResource("classpath:/META-INF/spring/windows.xml")
public class WindowsConfig {

private @Value("${test}") String message;

public @Bean
ExampleService service() {
    ExampleService service = new ExampleService();
    service.setMessage(message);
    return service;
}
}

linux.xml:

linux.xml:

<context:property-placeholder location="classpath:/META-INF/spring/linux.properties" />

windows.xml:

windows.xml:

<context:property-placeholder location="classpath:/META-INF/spring/windows.properties" />

也许可以进一步增强它,但是只是想向您展示根据操作系统使用某些xml文件的另一种想法.

Maybe it can be enhanced more, but just wanted to show you another idea of using certain xml files depending on the OS.

这篇关于Spring表达式评估操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆