zoukankan      html  css  js  c++  java
  • Spring Boot使用@ConfigurationProperties 读取自定义的properties的方法

    Spring Boot可使用注解的方式将自定义的properties文件映射到实体bean中,比如config.properties文件

    config.name=configname
    config.password=configpassword

    建对应的bean

    @Configuration
    @ConfigurationProperties(prefix = "config")
    @PropertySource("classpath:/config.properties")
    public class Config {

        private String name;
    
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }

    然后只需要在Spring Boot的启动类上面加注解
    @EnableConfigurationProperties({Config.class})  
    使用的时候直接注入即可
    @Autowired
    Config config;

    相关依赖:

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-configuration-processor</artifactId>
       <optional>true</optional>
    </dependency>
  • 相关阅读:
    js 和 jquery的宽高
    client、offset、scroll
    web开发中会话跟踪的方法有哪些
    前端需要注意哪些SEO
    ES6 Set和Map数据结构
    ES6实现数组去重
    ES6 Symbol
    ES6对象的拓展
    ES6数组的拓展
    ES6函数的拓展
  • 原文地址:https://www.cnblogs.com/mark8080/p/7986718.html
Copyright © 2011-2022 走看看