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

  • 相关阅读:
    Nacos 1.3.0版本部署连接mysql 8+
    Java Certificate证书问题
    UIKit之浅析UIButton
    Xcode Coule not launch "aaa" press launch failed:timed out waiting for app launch
    Cocos2d-x 安装教程for mac(Xcode)
    关于继承UITableViewController若干问题
    Table的分割线偏移量设置 及其 UIEdgeInset详解
    retain、strong、weak、assign区别
    iOS 使用xib创建cell的两种初始化方式
    No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)
  • 原文地址:https://www.cnblogs.com/zrbfree/p/7412931.html
Copyright © 2011-2022 走看看