• 七、自定义配置


    1、自定义条件

    a>构造条件

    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();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    b>创建不同条件下bean的类

    MessagePrint

    package com.yzh.boot.conditional;
    public interface MessagePrint {
    	public String showMessage();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    MyMessagePrint

    package com.yzh.boot.conditional;
    public class MyMessagePrint implements MessagePrint{
    	@Override
    	public String showMessage() {
    		return "test.properties文件存在。";
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    YourMessagePrint

    package com.yzh.boot.conditional;
    public class YourMessagePrint implements MessagePrint{
    	@Override
    	public String showMessage() {
    		return "test.properties文件不存在!";
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    c>创建配置类

    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();
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    d>创建测试类

    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());
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、自定义Starters

    a>新建项目

    b>修改pom文件

    <?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>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

    c>创建属性配置类MyProperties

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    d>创建判断依据类MyService

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    e>创建自动配置类MyAutoConfiguration

    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    f>注册配置

    在这里插入图片描述

    #若有多个自动配置,用逗号隔开
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.yzh.mystarters.MyAutoConfiguration

  • 相关阅读:
    数据库数据表设计思想一
    Ansible中的角色使用
    数据结构 - 图
    LabVIEW中EPICS客户端/服务端的测试
    借助实例,轻松掌握 Makefile
    bat文件与Vbs文件之间的常用操作(获取用户输入,执行VBS文件)
    SpringBoot集成Activiti7
    九章云极DataCanvas公司入选《AIGC产业链北京专精特新企业图谱》
    【区块链】讲解
    38、Java 中的正则表达式(单字符匹配和预定义字符)
  • 原文地址:https://blog.csdn.net/qq_52297656/article/details/126050691