spring boot中的条件装配bean的实现
条件装配
从SpringFramework3.1开始,允许在Bean装配时增加前置条件判断。
啥是条件装配
在bean装配前的条件判断。比如@Profile(是在spring3.1中引入),@Contditional(spring4.0中引入)
实现方式:注解方式,编程方式。
假设我们现在有一个多数据求和计算的小需求,定义两种方式Java7和Java8,然后使用条件装配的方式分别装配不同的bean。
首先我们定义一个接口
publicinterfaceCalculateService{ /** *从多个整数sum求和 *@paramvalues多个整数 *@returnsum累加值 */ Integersum(Integer...values); }
其次是两种不同的实现方式,Java7的方式
@Profile("Java7") @Service publicclassJava7CalculateServiceimplementsCalculateService{ @Override publicIntegersum(Integer...values){ System.out.println("Java7for循环实现"); intsum=0; for(inti=0;iJava8的实现
@Profile("Java8") @Service publicclassJava8CalculateServiceimplementsCalculateService{ @Override publicIntegersum(Integer...values){ System.out.println("Java8Lambda实现"); intsum=Stream.of(values).reduce(0,Integer::sum); returnsum; } publicstaticvoidmain(String[]args){ CalculateServicecalculateService=newJava8CalculateService(); System.out.println(calculateService.sum(1,2,3,4,5,6,7,8,9,10)); } }我们在这里使用了@Profile("Java8")注解来表明对应的profile。然后我定义一个启动类在里面配置装配哪一个bean
@SpringBootApplication(scanBasePackages="com.service") publicclassCalculateServiceBootstrap{ publicstaticvoidmain(String[]args){ ConfigurableApplicationContextcontext=newSpringApplicationBuilder(CalculateServiceBootstrap.class) .web(WebApplicationType.NONE) .profiles("Java8") .run(args); //CalculateServiceBean是否存在 CalculateServicecalculateService=context.getBean(CalculateService.class); System.out.println("calculateService.sum(1...10):"+ calculateService.sum(1,2,3,4,5,6,7,8,9,10)); //关闭上下文 context.close(); } }使用基于@ConditionalOnSystemProperty注解的方式实现。
首先我们定义一个注解,这里定义了一个属性和一个值。
/** *Java系统属性条件判断 * */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD}) @Documented @Conditional(OnSystemPropertyCondition.class) public@interfaceConditionalOnSystemProperty{ /** *Java系统属性名称 *@return */ Stringname(); /** *Java系统属性值 *@return */ Stringvalue(); }定义一个条件判断的类,当这个类中的条件满足是才会装载bean,这个实现类实现org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以获取的到注解中的name和value信息,假设我们现在实现判断系统属性和注解中的配置的一样就加载bean,System.getProperty("user.name")获取当前系统下的用户名,我的mac创建的用户名叫yanghongxing,如果我们在注解中配置的value是yanghongxing则装载这个bean。
/** *系统属性条件判断 * */ publicclassOnSystemPropertyConditionimplementsCondition{ @Override publicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata){ Mapattributes=metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName()); StringpropertyName=String.valueOf(attributes.get("name")); StringpropertyValue=String.valueOf(attributes.get("value")); StringjavaPropertyValue=System.getProperty(propertyName); returnpropertyValue.equals(javaPropertyValue); } } 最后在启动类中启动
publicclassConditionalOnSystemPropertyBootstrap{ @Bean @ConditionalOnSystemProperty(name="user.name",value="yanghongxing") publicStringhelloWorld(){ return"Hello,WorldHonson"; } publicstaticvoidmain(String[]args){ ConfigurableApplicationContextcontext=newSpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class) .web(WebApplicationType.NONE) .run(args); //通过名称和类型获取helloWorldBean StringhelloWorld=context.getBean("helloWorld",String.class); System.out.println("helloWorldBean:"+helloWorld); //关闭上下文 context.close(); } }我们可以在OnSystemPropertyCondition实现复杂的装载类的条件,判断是否装载某个bean。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。