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,加载配置文件可能有些不同!

  • 相关阅读:
    java相关知识集锦
    adb相关基础知识集锦
    我所理解的OOP——UML六种关系
    据说每个大牛、小牛都应该有自己的库——DOM处理续
    据说每个大牛、小牛都应该有自己的库——DOM处理
    JavaScript 继承
    据说每个大牛、小牛都应该有自己的库——Event处理
    据说每个大牛、小牛都应该有自己的库——框架篇
    可拖动的DIV续
    CSS hack前传——背景图片全屏
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7412931.html
Copyright © 2011-2022 走看看