zoukankan      html  css  js  c++  java
  • springboot 使用@ConfigurationProperties注入配置属性

    导入依赖,让springboot支持@ConfigurationProperties 注解

        <!-- 支持 @ConfigurationProperties 注解 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <version>{SpringBoot的版本}</version>
            </dependency>

    配置文件:

    connection:
      username: admin
      password: kyjufskifas2jsfs
      remoteAddress: 192.168.1.1

    配置文件注册类:

    @Component
    @ConfigurationProperties(prefix="connection")
    public class Properties {
        private String username;
        private String remoteAddress;
        private String password ;
    
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getRemoteAddress() {
            return remoteAddress;
        }
        public void setRemoteAddress(String remoteAddress) {
            this.remoteAddress = remoteAddress;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    
    }

    使用时直接注入即可:

    @RequestMapping("/test")
    @RestController
    public class TestController {
    
        @Autowired
        private Properties properties;
    
        @GetMapping("/test")
        public void test(){
            System.out.println("===================="+properties.getPassword());//====================kyjufskifas2jsfs
        }
    }
  • 相关阅读:
    自定义类型
    基本类型
    个人的职业规划
    FastDFS .Net客户端使用指南
    Delphi中资源的简单应用
    GridView数据绑定
    GridView的分页功能
    硬盘最多能分几个区?
    C#中public、private、protected、internal、protected internal
    2007.10.09错误记录
  • 原文地址:https://www.cnblogs.com/huanghuanghui/p/10759538.html
Copyright © 2011-2022 走看看