网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

springboot中怎么利用bean实现条件装配

本篇文章为大家展示了springboot中怎么利用bean实现条件装配,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

10余年的寿县网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整寿县建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“寿县网站设计”,“寿县网站推广”以来,每个客户项目都认真落实执行。

条件装配

从Spring Framework 3.1开始,允许在Bean装配时增加前置条件判断。

啥是条件装配

在bean装配前的条件判断。比如@Profile(是在spring3.1中引入),@Contditional(spring4.0中引入)实现方式:注解方式,编程方式。

假设我们现在有一个多数据求和计算的小需求,定义两种方式Java7和Java8,然后使用条件装配的方式分别装配不同的bean。

首先我们定义一个接口

public interface CalculateService {  /**   * 从多个整数 sum 求和   * @param values 多个整数   * @return sum 累加值   */  Integer sum(Integer... values);}

其次是两种不同的实现方式,Java7的方式

@Profile("Java7")@Servicepublic class Java7CalculateService implements CalculateService {  @Override  public Integer sum(Integer... values) {    System.out.println("Java 7 for 循环实现 ");    int sum = 0;    for (int i = 0; i < values.length; i++) {      sum += values[i];    }    return sum;  }}

Java8的实现

@Profile("Java8")@Servicepublic class Java8CalculateService implements CalculateService {  @Override  public Integer sum(Integer... values) {    System.out.println("Java 8 Lambda 实现");    int sum = Stream.of(values).reduce(0, Integer::sum);    return sum;  }  public static void main(String[] args) {    CalculateService calculateService = new Java8CalculateService();    System.out.println(calculateService.sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));  }}

我们在这里使用了@Profile("Java8")注解来表明对应的profile。然后我定义一个启动类在里面配置装配哪一个bean

@SpringBootApplication(scanBasePackages = "com.service")public class CalculateServiceBootstrap {  public static void main(String[] args) {    ConfigurableApplicationContext context = new SpringApplicationBuilder(CalculateServiceBootstrap.class)        .web(WebApplicationType.NONE)        .profiles("Java8")        .run(args);    // CalculateService Bean 是否存在    CalculateService calculateService = 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 @interface ConditionalOnSystemProperty {  /**   * Java 系统属性名称   * @return   */  String name();  /**   * Java 系统属性值   * @return   */  String value();}

定义一个条件判断的类,当这个类中的条件满足是才会装载bean,这个实现类实现org.springframework.context.annotation.Condition,AnnotatedTypeMetadata可以获取的到注解中的name和value信息,假设我们现在实现判断系统属性和注解中的配置的一样就加载bean,System.getProperty("user.name")获取当前系统下的用户名,我的mac创建的用户名叫yanghongxing,如果我们在注解中配置的value是yanghongxing则装载这个bean。

/** * 系统属性条件判断 * */public class OnSystemPropertyCondition implements Condition {  @Override  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {    Map attributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());    String propertyName = String.valueOf(attributes.get("name"));    String propertyValue = String.valueOf(attributes.get("value"));    String javaPropertyValue = System.getProperty(propertyName);    return propertyValue.equals(javaPropertyValue);  }}

最后在启动类中启动

public class ConditionalOnSystemPropertyBootstrap {  @Bean  @ConditionalOnSystemProperty(name = "user.name", value = "yanghongxing")  public String helloWorld() {    return "Hello,World Honson";  }  public static void main(String[] args) {    ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class)        .web(WebApplicationType.NONE)        .run(args);    // 通过名称和类型获取 helloWorld Bean    String helloWorld = context.getBean("helloWorld", String.class);    System.out.println("helloWorld Bean : " + helloWorld);    // 关闭上下文    context.close();  }}

我们可以在OnSystemPropertyCondition实现复杂的装载类的条件,判断是否装载某个bean。

上述内容就是springboot中怎么利用bean实现条件装配,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


分享标题:springboot中怎么利用bean实现条件装配
网站URL:http://bjjierui.cn/article/ppgpeh.html

其他资讯