什么是条件注解
@Conditional
根据满足某一个特定条件创建一个特定的 Bean
。就是根据特定条件来控制 Bean
的创建行为,这样我们可以利用这个特性进行一些自动的配置。
Spring boot 中大量用到了条件注解
简单实践
以不同的操作系统作为条件,我们将通过实现 CommandCondition
接口,并重写其 matches()
方法来构造判断条件。若在 Windows 系统下运行程序,则输出列表命令为 dir
,若在 Linux 系统或者Mac下运行程序,则输出列表命令为 ls
创建工程
这里我们只创建简单的maven工程,而不是spring boot工程。
- 打开idea,选择创建工程。
- 向导界面,选择Maven→勾选create from archetype,选择
org.apache.maven.archetypes:maven-archetype-quickstart
- 输入工程信息
- 设置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://lisen.cc
*/
public interface CommandCondition {
/**
* 获取命令
* @return win返回dir,linux及mac返回ls
*/
public String getCommand();
}
定义接口实现类
定义三个接口实现类,分别继承CommandCondition
接口
/**
* @author laughing
* @date 2020/9/26
* @site https://lisen.cc
*/
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://lisen.cc
*/
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://lisen.cc
*/
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://lisen.cc
*/
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://lisen.cc
*/
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://lisen.cc
*/
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://lisen.cc
*/
@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 命令运行的。
感谢感谢
感谢站长分享