美文网首页springboot
springboot使用注解方式加载配置文件

springboot使用注解方式加载配置文件

作者: John_Phil | 来源:发表于2019-07-29 21:59 被阅读0次

    我们在以往将配置信息从 xml或者properties文件 通过类加载器classloader 加载properties文件,将有用的信息读出,再放到对应的位置进行使用
    而在spring3.x中 提供了使用注解的方式进行配置读取

    @Configuration:声明当前类作为配置类使用

    @Bean:声明在方法上,将方法的返回值加入Bean容器

    @value:属性值注入

    @PropertySource:指定外部待加载文件

    快速搭建springboot项目以下为pom.xml

    <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.6.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.neusoft</groupId>
        <artifactId>springtest1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springtest1</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.11</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
           <!-- <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.5.RELEASE</version>
            </dependency>-->
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
          <!--  <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.6.RELEASE</version>
            </dependency>-->
    
    
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    

    aaa.properties配置文件放到resources中


    image.png
    aaa.a1=aa1123
    aaa.a2=aa2123
    aaa.a3=aa3123
    aaa.a4=aa4123
    

    再创建一个实体类


    路径
    package com.neusoft.pojo;
    public class ConfigPojo {
        public String a1=null;
        public  String a2=null;
        public  String a3=null;
        public  String a4=null;
    }
    

    我们先创建一个Configtest类

    package com.neusoft.config;
    
    import com.neusoft.pojo.ConfigPojo;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @PropertySource("classpath:testaa.properties")
    public class Configtest {
    
        @Value("${aaa.a1}")
        String a1;
        @Value("${aaa.a1}")
        String a2;
        @Value("${aaa.a3}")
        String a3;
        @Value("${aaa.a4}")
        String a4;
    
        @Bean
        public ConfigPojo getAaa(){
            ConfigPojo cp=new ConfigPojo();
            cp.a1=a1;
            cp.a2=a2;
            cp.a3=a3;
            cp.a4=a4;
    
            return cp;
        }
    }
    

    最后再创建一个control 方便请求与访问

    package com.neusoft.control;
    
    import com.neusoft.pojo.ConfigPojo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    
    @RestController
    public class HelloControl {
        @Autowired
        private ConfigPojo configPojo;
        @GetMapping("getconfig")
        public String test1(){
            return configPojo.a1+configPojo.a2+configPojo.a3+configPojo.a4;
        }
    }
    
    
    请求与响应信息

    第二种方式 可以使用lombok插件
    使用方式 https://www.jianshu.com/p/542bf23a9421

    将配置放到 application.properties


    image.png

    新建类 configlombok
    使用 @ConfigurationProperties(prefix = "aaa") 并指定前缀名称

    package com.neusoft.config;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "aaa")
    @Data
    public class Configlombok {
        public String a1;
        public String a2;
        public String a3;
        public String a4;
    

    修改Configtest
    @EnableConfigurationProperties(Configlombok.class) 允许配置生效

    package com.neusoft.config;
    
    import com.neusoft.pojo.ConfigPojo;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @EnableConfigurationProperties(Configlombok.class)
    public class Configtest {
    
        public ConfigPojo getAaa(Configlombok configlombok){
            ConfigPojo cp=new ConfigPojo();
            cp.a1=configlombok.getA1();
            cp.a2=configlombok.getA2();
            cp.a3=configlombok.getA3();
            cp.a4=configlombok.getA4();
            return cp;
        }
    }
    

    实体类增加 toString()方法

    package com.neusoft.pojo;
    
    public class ConfigPojo {
    
        public String a1=null;
        public String a2=null;
        public String a3=null;
        public String a4=null;
    
        @Override
        public String toString() {
            return a1+a2+a3+a4;
        }
    }
    

    修改control

    package com.neusoft.control;
    
    import com.neusoft.config.Configlombok;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    @RestController
    public class HelloControl {
        @Autowired
        private Configlombok configlombok;
        @GetMapping("getconfig")
        public String test1(){
            return configlombok.toString();
        }
    }
    

    再次访问


    image.png

    升级方法二 进一步简化

    在Configlombok中添加生成tostring注解
    并将@ConfigurationProperties(prefix = "aaa")注释掉

    package com.neusoft.config;
    
    import lombok.Data;
    import lombok.ToString;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    //@ConfigurationProperties(prefix = "aaa")
    @ToString(includeFieldNames=true)
    @Data
    public class Configlombok {
        public String a1;
        public String a2;
        public String a3;
        public String a4;
    }
    
    

    再将 Configtest 中的//@EnableConfigurationProperties({Configlombok.class}) 注释掉
    以及getAaa方法注释掉。
    并添加新的getAaa方法与注解
    @ConfigurationProperties(prefix = "aaa") 会根据返回的对象 到配置文件中aaa前缀下的名字与值 并去找 与 对象Configlombok中set 方法对应方法进行赋值 (自动将配置文件中的内容 注入到Configlombok对象属性中)

    package com.neusoft.config;
    
    import com.neusoft.pojo.ConfigPojo;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    //@PropertySource("classpath:testaa.properties")
    //@EnableConfigurationProperties({Configlombok.class})
    public class Configtest {
    
    //    @Value("${aaa.a1}")
    //    String a1;
    //    @Value("${aaa.a1}")
    //    String a2;
    //    @Value("${aaa.a3}")
    //    String a3;
    //    @Value("${aaa.a4}")
    //    String a4;
    
    
    //    public ConfigPojo getAaa(Configlombok configlombok){
    //        ConfigPojo cp=new ConfigPojo();
    //        cp.a1=configlombok.getA1();
    //        cp.a2=configlombok.getA2();
    //        cp.a3=configlombok.getA3();
    //        cp.a4=configlombok.getA4();
    //        return cp;
    //    }
        @Bean
        @ConfigurationProperties(prefix = "aaa")
        public Configlombok getAaa(){
            return new Configlombok();
        }
        
    
    }
    
    
    package com.neusoft.control;
    
    import com.neusoft.config.Configlombok;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    import javax.annotation.Resource;
    @RestController
    public class HelloControl {
    //    @Autowired
    //    private ConfigPojo configPojo;
        @Autowired
        private Configlombok configlombok;
        @GetMapping("getconfig")
        public String test1(){
            return configlombok.toString();
        }
    }
    
    访问输出结果
    4.配置文件为 application.yaml或者application.yml
    当appliation.yml与application.properties并存时 优先选择application.properties 如果properties 里没有 再选择yml
    如果再加入外置配置文件
    优先顺序 application.properties>appliation.yml>外置配置
    image.png

    appliction.properties

    aaa.a1=aa1123
    aaa.a2=aa2123
    aaa.a3=aa3123
    #aaa.a4=aa4123
    

    application.yml

    aaa:
      a1: bb1123
      a2: bb2123
      a3: bb3123
      a4: bb4123
    
    结果

    加入外置配置文件

    testaa.properties

    aaa.a1=cc1123
    aaa.a2=cc2123
    aaa.a3=cc3123
    aaa.a4=cc4123
    

    appliction.properties

    aaa.a1=aa1123
    aaa.a2=aa2123
    #aaa.a3=aa3123
    #aaa.a4=aa4123
    

    application.yml

    aaa:
      a1: bb1123
      a2: bb2123
      a3: bb3123
     # a4: bb4123
    

    Configtest 加入@PropertySource("classpath:testaa.properties")

    package com.neusoft.config;
    
    import com.neusoft.pojo.ConfigPojo;
    import lombok.Data;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @PropertySource("classpath:testaa.properties")
    //@EnableConfigurationProperties({Configlombok.class})
    public class Configtest {
    
    //    @Value("${aaa.a1}")
    //    String a1;
    //    @Value("${aaa.a1}")
    //    String a2;
    //    @Value("${aaa.a3}")
    //    String a3;
    //    @Value("${aaa.a4}")
    //    String a4;
    
    
    //    public ConfigPojo getAaa(Configlombok configlombok){
    //        ConfigPojo cp=new ConfigPojo();
    //        cp.a1=configlombok.getA1();
    //        cp.a2=configlombok.getA2();
    //        cp.a3=configlombok.getA3();
    //        cp.a4=configlombok.getA4();
    //        return cp;
    //    }
        @Bean
        @ConfigurationProperties(prefix = "aaa")
        public Configlombok getAaa(){
            return new Configlombok();
        }
    
    
    }
    
    
    image.png

    相关文章

      网友评论

        本文标题:springboot使用注解方式加载配置文件

        本文链接:https://www.haomeiwen.com/subject/xpxwrctx.html