SpringBoot入门系列片(九):使用自定义的属性并配置成类

前情提要

在SpringBoot中使用application.properties默认了很多配置,有时候我们也需要到该属性配置文件中自定义属性值,甚至于将一系列自定义属性合并成一个对象,而SpringBoot中也提供了这种功能的实现,那么现在就来试试吧


使用自定义的属性并配置成类

首先在applicatiion.properties属性文件中自定义一系列属性:
//application.properties配置文件
person.id=1
person.name=chengxi
person.nick=成兮
person.pass=970624
然后创建一个entity实体类,这里就创建一个person类吧,并使用注解ConfigurationProperties修饰,表示该类通过属性文件进行配置,代码如下:
package org.framework.demo.section1;

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

@ConfigurationProperties(prefix = "person")
public class Person {

    private Integer id;
    private String name;
    private String nick;
    private String pass;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
}
这样一来,Person类的属性值在运行时就已经确定值了。如果想要配置的属性值不是application.properties,而是我们自定义的属性文件中,直接将ConfigurationProperties注解中的location的值设置成该属性文件的位置即可。例如我设置application.properties同级下的person.properties文件:
//classpath: person.properties
person.id=1
person.name=chengxi
person.nick=成兮
person.pass=970624
然后在对应的Person类中的注解里面额外设置一个属性locations的值:
package org.framework.demo.section1;

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

@ConfigurationProperties(prefix = "person", locations="classpath:person.properties")
public class Person {

    private Integer id;
    private String name;
    private String nick;
    private String pass;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
}
而SpringBoot1.5版本以后取消了ConfigurationProperties注解的location属性的设置,所以如果我们想要自定义属性文件的位置,需要结合Component注解和PropertySource注解的帮助,代码如下:
package org.framework.demo.section1;

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

/**
 * SpringBoot1.5后的自定义属性文件的配置测试类
**/
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource("classpath: person.properties")
public class Person {

    private Integer id;
    private String name;
    private String nick;
    private String pass;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }
}
最后,我们需要在启动类中添加Person.class的配置,通过EnableConfigurationProperties注解进行配置,代码如下:
package org.framework.demo.section1;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;

/**
 * 在SpringBoot中简单使用Servlet
 * 通过注解注册Servlet
 * @author chengxi
 */
@SpringBootApplication
//用于配置文件自定义属性成类
@EnableConfigurationProperties(Person.class)
public class ServletApp2 {

    public static void main(String[] args){

        //关闭banner的输出
        SpringApplication app = new SpringApplication(ServletApp2.class);
//        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}
这样一来,即可实现与普通的entity实体类一样的功能了,在后面想要使用Person类的实例直接Autowired自动注入即可,如下:
@Autowired
private Person person;


猜你喜欢

转载自blog.csdn.net/qq_27905183/article/details/79080409