本文实例讲述了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程序设计有所帮助。