SpringBoot自定义配置

SpringBoot可以识别yml文件与properties文件,自定义配置最好使用properties格式,位于src/main/resource目录下的application.properties配置文件会被SpringBoot自动加载

@Value("${property}")获取配置属性

在application.properties配置文件中添加nikename=admin属性



通过@Value("${property}")注解获取配置nikename属性值

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

@RestController
@RequestMapping("user")
public class UserControll {
    
    @Value("${nikename}")
    private String nikename;
    
    @GetMapping("getUser")
    public String getUserName(){
        return nikename;
    }
    
}

通过将配置属性注入到JavaBean中,并通过JavaBean获取自定义属性

添加自定义属性

book.bookName=A Brief History Of Time
book.author=Stephen Hawkings

新建一个配置类SystemConfiguration.java通过@EnableConfigurationProperties(BookProperties.class)注解把使用 @ConfigurationProperties 的类进行注入

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(BookProperties.class)
public class SystemConfiguration {
    
}

新建一个book属性的javabean映射类

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

@ConfigurationProperties(prefix="book")
public class BookProperties {
    
    private String bookName;
    
    private String author;

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

}

WEB访问

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

@RestController
@RequestMapping("user")
public class UserControll {
    
    @Autowired
    private BookProperties bookProperties;
    
    @GetMapping("getUser")
    public String getUserName(){
        return bookProperties.getBookName()+" - by "+bookProperties.getAuthor();
    }
    
}

使用自定义配置文件将配置属性注入到JavaBean中,并通过JavaBean获取自定义属性

新建user.properties配置文件

system.user.name=zhangsan
system.user.sex=man
system.user.age=18


通过@PropertySource("classpath:user.properties")加载指定位置的properties文件

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

@Component
@ConfigurationProperties(prefix="system.user")
@PropertySource("classpath:user.properties")
public class UserProperties {

    private String name;
    
    private String sex;
    
    private String age;

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
    
}

WEB访问

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

@RestController
@RequestMapping("user")
public class UserControll {
    
    @Autowired
    private UserProperties userProperties;
    
    @GetMapping("getUser")
    public String getUserName(){
        return "name:"+userProperties.getName()+", age:"+userProperties.getAge()+", sex:"+userProperties.getSex();
    }
    
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容