zoukankan      html  css  js  c++  java
  • 170705、springboot编程之自定义properties

    spring boot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,可以这样用,如下!

    在application.properties文件中增加信息

    1、在application.properties配置文件增加

    ##自定义属性
    rick.name=rick
    rick.age=30

    2、自定义配置类RickProperties

    package com.rick.common.properties;
    
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "rick")
    public class RickProperties {
    
        private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }

    3、编写PropertiesController类

    package com.rick.apps.controller;
    
    import com.rick.common.ResultJson;
    import com.rick.common.properties.RickProperties;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class PropertiesController {
    
        @Autowired
        private RickProperties rickProperties;
    
    
        @GetMapping(value = "/rick")
        public ResultJson rick(){
            System.out.println("name===" + rickProperties.getName());
            System.out.println("age====" + rickProperties.getAge());
            return ResultJson.buildSuccessInstance();
        }
    
    }

    4、在主函数增加扫描注解@EnableConfigurationProperties

    启动项目,访问http://localhost:8080/rick,查看是否输出内容

    在自定义的properties文件中增加信息

    1、在resources下新增anna.properties文件

    ##自定义属性
    anna.name=anna
    anna.age=20

    2、创建AnnaProperties文件

    package com.rick.common.properties;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @Configuration
    @PropertySource("classpath:anna.properties")
    @ConfigurationProperties(prefix = "anna")
    public class AnnaProperties {
        private String address;
        private String gender;
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    }

    3、编写PropertiesController类

    package com.rick.apps.controller;
    
    import com.rick.common.ResultJson;
    import com.rick.common.properties.AnnaProperties;
    import com.rick.common.properties.RickProperties;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class PropertiesController {
    
        @Autowired
        private RickProperties rickProperties;
    
        @Autowired
        private AnnaProperties annaPropertes;
    
        @GetMapping(value = "/rick")
        public ResultJson rick() {
            System.out.println("rick.name===" + rickProperties.getName());
            System.out.println("rick.age====" + rickProperties.getAge());
            return ResultJson.buildSuccessInstance();
        }
    
        @GetMapping(value = "/anna")
        public ResultJson anna() {
            System.out.println("rick.name===" + rickProperties.getName());
            System.out.println("rick.age====" + rickProperties.getAge());
            System.out.println("anna.address===" + annaPropertes.getAddress());
            System.out.println("anna.gender====" + annaPropertes.getGender());
            return ResultJson.buildSuccessInstance();
        }
    
    }

    4、启动服务访问http://localhost:8080/anna

    注:不同版本的springboot,加载配置文件可能有些不同!

  • 相关阅读:
    Easy | LeetCode 108. 将有序数组转换为二叉搜索树
    Medium | LeetCode 105 | 剑指 Offer 07. 从前序与中序遍历序列构造二叉树
    Easy | LeetCode 543. 二叉树的直径
    Easy | LeetCode 235 | 剑指 Offer 68
    Easy | LeetCode 236 | 剑指 Offer 68
    Medium | LeetCode 114. 二叉树展开为链表 | 先序遍历 | 递归 | 迭代
    Medium | LeetCode 538,1038. 把二叉搜索树转换为累加树
    Medium | LeetCode 230. 二叉搜索树中第K小的元素
    Easy | 剑指 Offer 54. 二叉搜索树的第k大节点
    stl(5)vector容器
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7412931.html
Copyright © 2011-2022 走看看