MyCondition
package com.yzh.boot.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class MyCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getResourceLoader().getResource("classpath:test.properties").exists();
}
}
YourCondition
package com.yzh.boot.conditional;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class YourCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return !context.getResourceLoader().getResource("classpath:test.properties").exists();
}
}
MessagePrint
package com.yzh.boot.conditional;
public interface MessagePrint {
public String showMessage();
}
MyMessagePrint
package com.yzh.boot.conditional;
public class MyMessagePrint implements MessagePrint{
@Override
public String showMessage() {
return "test.properties文件存在。";
}
}
YourMessagePrint
package com.yzh.boot.conditional;
public class YourMessagePrint implements MessagePrint{
@Override
public String showMessage() {
return "test.properties文件不存在!";
}
}
package com.yzh.boot.conditional;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration //标注这是一个配置文件
public class ConditionConfig {
@Bean
@Conditional(MyCondition.class)
public MessagePrint myMessage() {
return new MyMessagePrint();
}
@Bean
@Conditional(YourCondition.class)
public MessagePrint yourMessage() {
return new YourMessagePrint();
}
}
package com.yzh.boot.conditional;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestMain {
private static AnnotationConfigApplicationContext context;
public static void main(String[] args) {
context = new AnnotationConfigApplicationContext(ConditionConfig.class);
MessagePrint mp = context.getBean(MessagePrint.class);
System.out.println(mp.showMessage());
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yzh</groupId>
<artifactId>mystarters</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mystarters</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!--springboot自身的自动配置依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.yzh.mystarters.MystartersApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.yzh.mystarters;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "my")
public class MyProperties {
private String msg="默认值";
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg=msg;
}
}
package com.yzh.mystarters;
public class MyService {
private String msg;
public String sayMsg(String msg){
return "my "+ this.msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getMsg() {
return msg;
}
}
package com.yzh.mystarters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
//开启属性配置类MyProperties提供参数
@EnableConfigurationProperties(MyProperties.class)
//类路径中是否存在对应的类
@ConditionalOnBean(MyService.class)
//应用环境中属性是否存在指定的值
@ConditionalOnProperty(prefix = "my",name = "msg",matchIfMissing = true)
public class MyAutoConfiguration {
@Autowired
private MyProperties myProperties;
@Bean
@ConditionalOnMissingBean(MyService.class)
public MyService myService(){
MyService myService = new MyService();
myService.sayMsg(myProperties.getMsg());
return myService;
}
}
#若有多个自动配置,用逗号隔开
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yzh.mystarters.MyAutoConfiguration