Spring条件注解用法案例分析

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

本文实例讲述了Spring条件注解用法。分享给大家供大家参考,具体如下:

一 点睛

Spring 4 提供了一个更通用的基于条件的Bean的创建,即使用@Conditional注解。

@Conditional根据满足仅一个特定条件创建一个特定的Bean。也就是根据特定的条件来控制Bean的创建行为,这样就可以利用这个特性进行一些自动的配置。

二 项目说明

以不同的操作系统为条件,通过实现@Condition接口,并重写matches方法来构造条件。若在windows系统下运行,则输出列表命令为dir;若在Linux操作系统下运行程序,则输出列表命令为ls。

三 实战

1 判断条件定义

1.1 windows的判定条件

package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class WindowsCondition implements Condition {
 public boolean matches(ConditionContext context,
   AnnotatedTypeMetadata metadata) {
  return context.getEnvironment().getProperty("os.name").contains("Windows");
 }
}

1.2 Linux的判定条件

package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class LinuxCondition implements Condition {
 public boolean matches(ConditionContext context,
   AnnotatedTypeMetadata metadata) {
  return context.getEnvironment().getProperty("os.name").contains("Linux");
 }
}

2 不同系统下的Bean类

2.1 接口

package com.wisely.highlight_spring4.ch3.conditional;
public interface ListService {
  public String showListCmd();
}

2.2 Window下创建的Bean类

package com.wisely.highlight_spring4.ch3.conditional;
public class WindowsListService implements ListService {
  @Override
  public String showListCmd() {
   return "dir";
  }
}

2.3 Linux下所创建的Bean类

package com.wisely.highlight_spring4.ch3.conditional;
public class LinuxListService implements ListService{
  @Override
  public String showListCmd() {
   return "ls";
  }
}

3 配置类

package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConditionConifg {
 @Bean
 @Conditional(WindowsCondition.class) //符合window条件,则实例化WindowsListService
 public ListService windowsListService() {
  return new WindowsListService();
 }
 @Bean
 @Conditional(LinuxCondition.class) //符合Linux条件,则实例化LinuxListService
 public ListService linuxListService() {
  return new LinuxListService();
 }
}

4 主类

package com.wisely.highlight_spring4.ch3.conditional;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
  public static void main(String[] args) {
   AnnotationConfigApplicationContext context =
    new AnnotationConfigApplicationContext(ConditionConifg.class);
   ListService listService = context.getBean(ListService.class);
   System.out.println(context.getEnvironment().getProperty("os.name")
     + "系统下的列表命令为: "
     + listService.showListCmd());
   context.close();
  }
}

四 运行

windows下运行结果如下:

Windows 10系统下的列表命令为: dir

五 扩展

如果把LinuxCondition条件改成和WindowsCondition一样的条件会怎样呢?即有两个条件都匹配会怎样呢?

修改后的代码如下:

public class LinuxCondition implements Condition {
 public boolean matches(ConditionContext context,
   AnnotatedTypeMetadata metadata) {
  return context.getEnvironment().getProperty("os.name").contains("Windows");
 }
}

修改后再运行,报错了:

Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.wisely.highlight_spring4.ch3.conditional.ListService] is defined: expected single matching bean but found 2: linuxListService,windowsListService
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:365)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)
    at com.wisely.highlight_spring4.ch3.conditional.Main.main(Main.java:11)

报错信息很明显:

[com.wisely.highlight_spring4.ch3.conditional.ListService] is defined: expected single matching bean but found 2: linuxListService,windowsListService

匹配到了两条,所以针对条件判断,设计程序时,只能匹配上一条,否则会抛异常。

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

更多精彩内容其他人还在看

JavaWeb项目部署到服务器详细步骤详解

这篇文章主要介绍了JavaWeb项目如何部署到服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA基于支付宝小程序搭建springboot项目的详细步骤

这篇文章主要介绍了IDEA基于支付宝小程序搭建springboot项目的详细步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解SpringBoot应用服务启动与安全终止

这篇文章主要介绍了SpringBoot应用服务启动与安全终止,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Boot启动及退出加载项的方法

这篇文章主要介绍了Spring Boot启动及退出加载项的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring Data Jpa 自动生成表结构的方法示例

这篇文章主要介绍了Spring Data Jpa 自动生成表结构的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

IDEA中osgi的开发应用指南详解

这篇文章主要介绍了IDEA中osgi的开发应用指南详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解用maven将dubbo工程打成jar包运行

这篇文章主要介绍了详解用maven将dubbo工程打成jar包运行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解Java合并数组的两种实现方式

这篇文章主要介绍了Java合并数组的两种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

使用Jenkins Pipeline自动化构建发布Java项目的方法

这篇文章主要介绍了使用Jenkins Pipeline自动化构建发布Java项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

使用Maven配置Spring的方法步骤

这篇文章主要介绍了使用Maven配置Spring的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多