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

上一篇:mac osx 升级yosemite后java出错的解决


下一篇:单元测试工具 - karma