SpringBoot中Condition註解條件的使用獲取配置文件內容

SpringBoot中Condition註解條件的使用

package com.longfor.plus.common.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.net.InetAddress;
import java.net.UnknownHostException;


public class LinuxCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        //可以判斷容器中的bean註冊情況,也可以給容器中註冊bean

        String port = environment.getProperty("server.port");
        String active = environment.getProperty("spring.profiles.active");
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String hostAddress = address.getHostAddress();
        System.out.println(port);
        System.out.println(hostAddress);
        if(property.contains("linux")){
            return true;
        }
        return false;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章