自定义属性配置

application.properties 提供自定义属性的支持,我们可以配置一些常量。

默认加载位置包含:
1、项目工程的根目录/;
2、项目工程根目录下的config/目录;
3、项目工程的resource/目录;
4、项目工程的resource/config/目录

一、application.properties

made.name=made
made.address=BeiJing
made.tel=15510000***

二、直接使用

package com.made.springBoot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.made.springBoot.common.ConfigProperties;
import com.made.springBoot.common.TestProperties;


@RestController // controller里面的方法都以json格式输出
public class MainController {

    @Value("${made.name}")
    private String name;
    
    @Value("${made.address}")
    private String address;
    
    @Value("${made.tel}")
    private String tel;

    @RequestMapping("/test")
    public String test() {

        return name + address + tel;
    }
}

三、编写 Bean类,加载属性

有时候属性太多,影响代码可读性,我们可以在实体 Bean中加载出来

1. 使用 spring支持的 @Value
package com.made.springBoot.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ConfigProperties {

    @Value("${made.name}")
    private String name;
    
    @Value("${made.address}")
    private String address;
    
    @Value("${made.tel}")
    private String tel;

    @Override
    public String toString() {
        return "ConfigProperties [name=" + name + ", address=" + address + ", tel=" + tel + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
}

2.使用@ConfigurationProperties(prefix="") 设置前缀,属性上不需要添加注解
package com.made.springBoot.common;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component // == spring Boot入口类上加 @EnableConfigurationProperties({ConfigProperties .class})
@ConfigurationProperties(prefix="made")
public class ConfigProperties {

    private String name;
    
    private String address;
    
    private String tel;

    @Override
    public String toString() {
        return "ConfigProperties [name=" + name + ", address=" + address + ", tel=" + tel + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    
}

3. 使用

在controller中注入并使用 ConfigProperties 这个 Bean。

package com.made.springBoot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.made.springBoot.common.ConfigProperties;
import com.made.springBoot.common.TestProperties;

//@Controller
@RestController // controller里面的方法都以json格式输出
public class MainController {

    @Autowired
    private ConfigProperties properties;
    

    @RequestMapping("/test")
    public String test() {

        System.out.println(properties);

        return "test";
    }
}

四、自定义配置文件

有时候我们不希望把所有配置都放在 application.properties里面,这时候我们可以另外定义一个 test.properties。

1. est.properties
made.test.name=mmm
made.test.address=china
made.test.tel=15510000***
2. 实体类 Bean
package com.made.springBoot.common;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:test.properties")
@ConfigurationProperties(prefix="made.test")
public class TestProperties {

    private String name;
    
    private String address;
    
    private String tel;
    
    

    @Override
    public String toString() {
        return "TestProperties [name=" + name + ", address=" + address + ", tel=" + tel + "]";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容