TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码

Spring Boot条件注解

Laughing博主
2020-09-27
/
0 评论
/
1,228 阅读
/
522 个字
/
百度已收录
09/27
本文最后更新于2024年03月21日,已超过182天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

什么是条件注解

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

Spring boot 中大量用到了条件注解

简单实践

以不同的操作系统作为条件,我们将通过实现 CommandCondition 接口,并重写其 matches() 方法来构造判断条件。若在 Windows 系统下运行程序,则输出列表命令为 dir ,若在 Linux 系统或者Mac下运行程序,则输出列表命令为 ls

创建工程

这里我们只创建简单的maven工程,而不是spring boot工程。

  1. 打开idea,选择创建工程。
  2. 向导界面,选择Maven→勾选create from archetype,选择org.apache.maven.archetypes:maven-archetype-quickstart
  3. 输入工程信息
  4. 设置maven

    引入依赖

    打开pom.xml文件,增加spring依赖

     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>5.2.9.RELEASE</version>
     </dependency>

    定义接口

    /**
     * 定义接口
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public interface CommandCondition {
    
     /**
      * 获取命令
      * @return win返回dir,linux及mac返回ls
      */
     public String getCommand();
    
    }

    定义接口实现类

    定义三个接口实现类,分别继承CommandCondition接口

    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public class MacCommand implements CommandCondition{
     /**
      * 获取命令
      *
      * @return win返回dir,linux及mac返回ls
      */
     @Override
     public String getCommand() {
         return "ls";
     }
    }
    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public class LinuxCommand implements CommandCondition{
     /**
      * 获取命令
      *
      * @return win返回dir,linux及mac返回ls
      */
     @Override
     public String getCommand() {
         return "ls";
     }
    }
    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public class WindowsCommand implements CommandCondition{
     /**
      * 获取命令
      *
      * @return win返回dir,linux及mac返回ls
      */
     @Override
     public String getCommand() {
         return "dir";
     }
    }

    定义条件类

    定义三个条件类,分别实现matches() 方法,用来构造判断条件

    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public class LinuxCommandContidion implements Condition {
     @Override
     public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
         return Objects.requireNonNull(conditionContext.getEnvironment().getProperty("os.name")).contains("Linux");
     }
    }
    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public class MacCommandContidion implements Condition {
     @Override
     public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
         return Objects.requireNonNull(conditionContext.getEnvironment().getProperty("os.name")).contains("Mac");
     }
    }
    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    public class WindowsCommandContidion implements Condition {
     @Override
     public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
         return Objects.requireNonNull(conditionContext.getEnvironment().getProperty("os.name")).contains("Windows");
     }
    }

    定义配置类

    定义配置类,注入根据条件注入Bean

    /**
     * @author laughing
     * @date 2020/9/26
     * @site https://www.xiangcaowuyu.net
     */
    @Configuration
    public class CommandConfig {
    
     @Bean
     @Conditional(LinuxCommandContidion.class)
     public CommandCondition linuxCommand(){
         return new LinuxCommand();
     }
    
     @Bean
     @Conditional(MacCommandContidion.class)
     public CommandCondition macCommand(){
         return new MacCommand();
     }
    
     @Bean
     @Conditional(WindowsCommandContidion.class)
     public CommandCondition windowsCommand(){
         return new WindowsCommand();
     }
     
    }

    使用

    在main函数中,创建一个 AnnotationConfigApplicationContext 实例用来加载 Java 配置类,并注册我们的配置类,然后刷新容器。容器刷新完成后,我们就可以从容器中去获取 CommandCondition 的实例了,这个实例会根据操作系统不同,返回不同的命令。

    /**
     * Hello world!
     */
    public class App {
    
     public static void main(String[] args) {
         AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
         annotationConfigApplicationContext.register(CommandConfig.class);
         annotationConfigApplicationContext.refresh();
         CommandCondition commandCondition = annotationConfigApplicationContext.getBean(CommandCondition.class);
         String command = commandCondition.getCommand();
         System.out.println(command);
     }
    }

    测试

    通过java -jar命令在不同操作系统分别运行jar包,查看输出

    java -jar conditional-1.0-SNAPSHOT-jar-with-dependencies.jar

生成jar包运行说明

jar包无法运行

默认情况下,直接生成的jar包是没有配置入口类并且不包含依赖的。所以会报以下错误

解决

我们需要在pom.xml中增加打包插件maven-assembly-plugin

<!-- 打包方式:mvn package assembly:single  -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.5.5</version>
        <configuration>
          <archive>
            <manifest>
              <mainClass>org.lisen.condition.App</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>assembly</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

重新 mvn install
我们会得到两个jar包,名称带-with-dependencies是包含依赖的,也是我们可以通过java -jar 命令运行的。

参考资料

条件注解,spring boot的基石

Spring Boot
朗读
赞(0)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

香草物语

本文链接:

https://www.xiangcaowuyu.net/java/spring-boot-conditional-annotation.html(转载时请注明本文出处及文章链接)

评论 (0)
  1. Crest_z 闲逛
    MacOS · Google Chrome

    感谢感谢

    2019-06-11 回复
  2. rbq 闲逛
    MacOS · Google Chrome

    感谢站长分享

    2018-09-26 回复