纵有疾风起
人生不言弃

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;@SpringBootApplicationpublic 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

 

 

 

 

文章转载于:https://www.cnblogs.com/lm970585581/p/9803965.html

原著是一个有趣的人,若有侵权,请通知删除

未经允许不得转载:起风网 » springBoot按条件装配:Condition

分享到: 生成海报
avatar

评论 抢沙发

评论前必须登录!

立即登录   注册

切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活