• SpringBoot加载配置文件


    方式一、

    import
    org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @PropertySource("classpath:config/my.prop") @ConfigurationProperties(prefix = "aaa") public class PropConf { private String a; private String b; private String c; public String getA() {return a;} public void setA(String a) {this.a = a;} public String getB() {return b;} public void setB(String b) {this.b = b;} public String getC() {return c;} public void setC(String c) {this.c = c;} public void show(){ System.out.println("a --- > " + a); System.out.println("b --- > " + b); System.out.println("c --- > " + c); } }

    方式二、

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyConf {
    
        @Value("${string.port}")     private int intPort;
        @Value("${string.port}")     private  String stringPort;
        @Value("${db.link.url}")     private String dbUrl;
        @Value("${db.link.driver}")  private String dbDriver;
        @Value("${db.link.username}")private String dbUsername;
        @Value("${db.link.password}")private String dbPassword;
    
        public void show(){
            System.out.println("===========================================");
            System.out.println("intPort :   " + (intPort + 1111));
            System.out.println("stringPort :   " + (stringPort + 1111));
            System.out.println("string :   " + dbUrl);
            System.out.println("string :   " + dbDriver);
            System.out.println("string :   " + dbUsername);
            System.out.println("string :   " + dbPassword);
            System.out.println("===========================================");
        }
    }

    方式三、

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    
    @Configuration
    @PropertySource("classpath:config/my.prop")
    @ConfigurationProperties(prefix = "aaa")
    public class PropConf {
    
        private String a;
        private String b;
        private String c;
    
        public void setA(String a) {  this.a = a;  }
        public void setB(String b) { this.b = b;  }
        public void setC(String c) {  this.c = c; }
    
        public void show(){
            System.out.println("a --- > " + a);
            System.out.println("b --- > " + b);
            System.out.println("c --- > " + c);
        }
    }
  • 相关阅读:
    9.16动手又动脑
    C#中集合的交集:Intersect问题
    LeetCode Easy: 27. Remove Element
    LeetCode Easy: 26.Remove Duplicates from Sorted Array
    LeetCode Easy: 21. Merge Two Sorted Lists
    LeetCode Easy: 20. Valid Parentheses
    LeetCode Easy: 14. Longest Common Prefix
    LeetCode Easy: 13. Roman to Integer
    LeetCode Easy: Palindrome Number
    DL: 初试 tensorflow
  • 原文地址:https://www.cnblogs.com/zk-blog/p/14292599.html
Copyright © 2020-2023  润新知