1. 程式人生 > >springBoot按條件裝配:Condition

springBoot按條件裝配:Condition

編碼格式轉換器介面

package com.boot.condition.bootconditionconfig.converter;

/**
 * 編碼格式轉換器介面
 */
public interface EncodingConverter {
}

GBK轉換器

 

package com.boot.condition.bootconditionconfig.converter;

public class GBKEncodingConverter implements EncodingConverter {
}

 

UTF-8轉換器

 

package com.boot.condition.bootconditionconfig.converter;

public class UTF8EncodingConverter implements EncodingConverter {
}

 

裝配配置類

package com.boot.condition.bootconditionconfig.config;

import com.boot.condition.bootconditionconfig.condition.GBKCondition;
import com.boot.condition.bootconditionconfig.condition.UTF8Condition;
import com.boot.condition.bootconditionconfig.converter.EncodingConverter; import com.boot.condition.bootconditionconfig.converter.GBKEncodingConverter; import com.boot.condition.bootconditionconfig.converter.UTF8EncodingConverter; import org.springframework.boot.SpringBootConfiguration; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional; /** * 使用按照條件裝配的過程: * 1、@Conditional [value值必須是實現Condition介面的類] * 2、根據介面中的matches方法的返回結果確定是否裝配bean物件到spring容器中 * false 不裝配 , true 就會裝配到spring容器中 */ @SpringBootConfiguration // @Conditional(UTF8Condition.class) public class EncodingConverterConfig { @Bean @Conditional(UTF8Condition.class) public EncodingConverter createUTF8Converter() { return new UTF8EncodingConverter(); } @Bean @Conditional(GBKCondition.class) public EncodingConverter createGBKConverter() { return new GBKEncodingConverter(); } }

GBK裝配條件類

 

package com.boot.condition.bootconditionconfig.condition;

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

public class GBKCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String encoding = System.getProperty("file.encoding");
        if ("gbk".equals(encoding.toLowerCase())) {
            return true;
        }
        return false;
    }
}

 

UTF-8裝配條件類

 

package com.boot.condition.bootconditionconfig.condition;

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

public class UTF8Condition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String encoding = System.getProperty("file.encoding");
        if ("utf-8".equals(encoding.toLowerCase())) {
            return true;
        }
        return false;
    }
}

 

測試類

package com.boot.condition.bootconditionconfig;

import com.boot.condition.bootconditionconfig.converter.EncodingConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.Map;

@SpringBootApplication
public class BootConditionConfigApplication {


    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                SpringApplication.run(BootConditionConfigApplication.class, args);

        Map<String, EncodingConverter> beansOfType = context.getBeansOfType(EncodingConverter.class);
        System.out.println(beansOfType);
        // 使用獲取系統的編碼格式的系統變數的key
        System.out.println(System.getProperty("file.encoding"));

        context.close();
    }
}

結果分析:

file.encoding=’gbk’,則只裝配GBKEncodingConverter

file.encoding=’utf-8’,則只裝配UTF8EncodingConverter