zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    InitializingBean
    执行jar的记事本
    vue中$forceUpdate()事件
    帆软时间检索限制90天
    vue清楚子组件v-model绑定的值
    Intellij IDEA中启动多个微服务(开启Run Dashboard管理)
    Java将CST的时间字符串转换成需要的日期格式字符串
    axios发送命令如何实现同步
    利用tomcat启动web前端
    vue生命周期
  • 原文地址:https://www.cnblogs.com/zk-blog/p/14292599.html
Copyright © 2011-2022 走看看