场景:需要根据系统的编码格式有选择装配类。
分析:最直接的实现方式,定义各种编码格式对应的处理类,可以通过System.getProperty("file.encoding")获得项目的编码格式,从而通过条件语句if...else..等方式做相应的业务处理,然后实际中一个项目往往只用到一种编码格式,如果把所有的编码处理类都装配到spring容器中,那么造成了浪费资源。有没有一种更好的方式可以根据需要只装配对象。
答案肯定是有的,SpringBoot框架为我们提供了@Conditional注解
原理:@Conditional实现的原理就是该注解的值就是一个继承了Condition的类的集合,而Condition中有个matches方法。当该方法返回
·true:对象被装配
·false:对象不被装配
1 @FunctionalInterface 2 public interface Condition { 3 boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2); 4 }
Condition源代码
demo代码
(1)代码结构
(2)GBKCondition.java类实现Condition的matches方法
1 package com.demo.boot.bootconditionconfig.condition; 2 3 import org.springframework.context.annotation.Condition; 4 import org.springframework.context.annotation.ConditionContext; 5 import org.springframework.core.type.AnnotatedTypeMetadata; 6 7 public class GBKCondition implements Condition { 8 @Override 9 public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { 10 String encoding = System.getProperty("file.encoding"); 11 return "gbk".equals(encoding.toLowerCase()); 12 } 13 }
GBKCondition.java
(3)UTF8Condition.java类实现Condition的matches方法
1 package com.demo.boot.bootconditionconfig.condition; 2 3 import org.springframework.context.annotation.Condition; 4 import org.springframework.context.annotation.ConditionContext; 5 import org.springframework.core.type.AnnotatedTypeMetadata; 6 7 public class UTF8Condition implements Condition { 8 @Override 9 public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { 10 String encoding = System.getProperty("file.encoding"); 11 return "utf-8".equals(encoding.toLowerCase()); 12 } 13 }
UTF8Condition.java
(4)EncodingConverterConfig.java配置类,将GBKCondition类和UTF8Condition类交给SpringBoot容器管理
1 package com.demo.boot.bootconditionconfig.config; 2 3 import com.demo.boot.bootconditionconfig.condition.GBKCondition; 4 import com.demo.boot.bootconditionconfig.condition.UTF8Condition; 5 import com.demo.boot.bootconditionconfig.converter.EncodingConverter; 6 import com.demo.boot.bootconditionconfig.converter.GBKEncodingConverter; 7 import com.demo.boot.bootconditionconfig.converter.UTF8EncodingConverter; 8 import org.springframework.boot.SpringBootConfiguration; 9 import org.springframework.context.annotation.Bean; 10 import org.springframework.context.annotation.Conditional; 11 12 @SpringBootConfiguration 13 //@Conditional({UTF8Condition.class,GBKCondition.class})//@Conditional同时也可以注解类,返回true时,类的注解方法都被装配,多个condition之间是“与”逻辑 14 public class EncodingConverterConfig { 15 @Bean 16 @Conditional(UTF8Condition.class) 17 public EncodingConverter createUTF8EncodingConverter() { 18 return new UTF8EncodingConverter(); 19 } 20 21 @Bean 22 @Conditional(GBKCondition.class) 23 public EncodingConverter createGBKEncodingConverter() { 24 return new GBKEncodingConverter(); 25 } 26 }
EncodingConverterConfig.java
(5)应用程序入口
1 package com.demo.boot.bootconditionconfig; 2 3 import com.demo.boot.bootconditionconfig.converter.EncodingConverter; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.context.ConfigurableApplicationContext; 7 8 import java.util.Map; 9 10 @SpringBootApplication 11 public class BootConditionConfigApplication { 12 13 public static void main(String[] args) { 14 ConfigurableApplicationContext context = SpringApplication.run(BootConditionConfigApplication.class, args); 15 16 Map<String, EncodingConverter> beansOfType = context.getBeansOfType(EncodingConverter.class); 17 System.out.println(beansOfType); 18 //获取系统编码格式变量 19 System.out.println(System.getProperty("file.encoding")); 20 21 context.close(); 22 } 23 24 }
BootConditionConfigApplication.java
(6)打印结果
{createUTF8EncodingConverter=co[email protected]3e2fc448} UTF-8
至此,实现了根据项目编码格式,有选择装配需要对象实例
原文地址:https://www.cnblogs.com/lfhappy/p/11028474.html
时间: 2024-11-08 12:39:52