복붙노트

[SPRING] OS를 평가하는 봄 표현

SPRING

OS를 평가하는 봄 표현

OS (운영 체제) 시스템 속성을 평가하여 해당 구성 파일을로드해야합니다. 예 : OS가 Windows로 평가하면 win.xml 속성이로드되거나 OS가 Unix 또는 Linux로 평가하면 unix.xml 속성이로드됩니다.

아래의 철자가 잘 작동합니다.

#{(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']를 평가하는 대신 변수에이 값을 넣고 조건을 일치 시키려고합니다. #이 변수 사용법을 보았습니다 (http://docs.spring.io/spring-framework/docs/3.0.6.RELEASE/spring-framework-reference/html/expressions.html 초 6.5.10.1). 아래의 주문

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

하지만 어쨌든, 그것은 구문 분석 예외를 제공하고 있습니다.

누구든지 무엇을 제안 할 수 있습니까?

해결법

  1. ==============================

    1.이 접근 방식은 어떻습니까? 스프링 4가 필요합니다.

    이 접근 방식은 어떻습니까? 스프링 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);
         }
    }
    

    그런 다음 위의 조건을 사용하여 환경에 따라로드 할 원하는 메서드 나 클래스에 주석을 추가 할 수 있습니다.

    @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 :

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

    Виндовс.хмл :

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

    어쩌면 더 많이 향상시킬 수 있지만, OS에 따라 특정 XML 파일을 사용하는 다른 아이디어를 보여 주기만하면됩니다.

  2. ==============================

    2.Spel 표현식 내에서 변수를 설정할 수 없습니다. 변수는 컨텍스트로 설정되어야합니다. 너 할 수있어.

    Spel 표현식 내에서 변수를 설정할 수 없습니다. 변수는 컨텍스트로 설정되어야합니다. 너 할 수있어.

    context.setVariable("os", SystemProperties.getProperty("os.name")); 
    

    Spel에서 #os로 사용하십시오.

  3. from https://stackoverflow.com/questions/23356757/spring-expression-to-evaluate-os by cc-by-sa and MIT license