17点目のSpring 4.1-@Comptional


17.1@Condtional
@Conditionalは条件によってspringを配置するbeanに対してサポートを提供しています.つまり、ある条件を満たすなら、どのように対応するbeanを配置しますか?アプリケーションシーン
あるjarがclassipathの中で包む時、あるいくつかのbeanを配置します.あるbeanが配置されたら、自動的に特定のbeanを配置します.環境変数が設定されている場合、あるbeanを作成します.@Conditionalは、敏捷開発の原則である「習慣は配置より優れている」ということを支持しています.@ConditionalはSpring Bootの快速開発フレームが「配置より慣習が優れている」コア技術を実現します.17.2例
デモはwindowsとlinuxシステムの下で、異なるbeanを初期化して、windowsとlinuxを判断条件とします.
17.2.1構造判断条件
条件の構造には、コンディショナーインターフェースを実現するクラスが必要です.
WindowsCondition
package com.wisely.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class WindowsCondition implements Condition {

    public boolean matches(ConditionContext context,
            AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }

}
LinuxCondition
package com.wisely.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition implements Condition {

    public boolean matches(ConditionContext context,
            AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("os.name").contains("Linux");
    }

}
17.2.2異なる条件の作成に必要なbean
インターフェース
package com.wisely.conditional;


public interface CommandService {

    public String showListCommand();
}
Windows CommnadService
package com.wisely.conditional;

public class WindowsCommnadService implements CommandService {

    public String showListCommand() {
        return "dir";
    }

}
LinuxCommandService
package com.wisely.conditional;

public class LinuxCommandService implements CommandService {

    public String showListCommand() {
        return "ls";
    }

}
17.2.3プロファイルの作成
package com.wisely.conditional;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DemoConfig {
    @Bean
    @Conditional(WindowsCondition.class)
    public CommandService commandService() {
        return new WindowsCommnadService();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public CommandService linuxEmailerService() {
        return new LinuxCommandService();
    }

}
17.2.4試験
windows下
package com.wisely.conditional;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext("com.wisely.conditional");
        CommandService cs = context.getBean(CommandService.class);
        System.out.println(cs.showListCommand());
        context.close();
    }
}
出力結果
dir
Linux下(この例はLinuxに切り替えられず、直接的にOS.nameをLinuxに修正する)
package com.wisely.conditional;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        System.setProperty("os.name", "Linux");
        AnnotationConfigApplicationContext context =  new AnnotationConfigApplicationContext("com.wisely.conditional");
        CommandService cs = context.getBean(CommandService.class);
        System.out.println(cs.showListCommand());
        context.close();

    }
}
出力結果
ls