资讯专栏INFORMATION COLUMN

spring boot学习(7)— 配置信息的获取方式

yy13818512006 / 918人阅读

1. 使用 ConfigurationProperties 来使用 properties 的值。

启用自定义配置: @Configuration @EnableConfigurationProperties({YourConfigClass}.class)

@ConfigurationProperties(prefix) 注解自定义的 YourConfigClass

通过 bean 来使用自定义的配置信息类

@SpringBootApplication
@EnableConfigurationProperties(TestConfigurationProperties.class)
public class DemoApplication{

    @Autowired
    TestConfigurationProperties testConfig;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        new DemoApplication().testConfig.printProperties();
    }

    @PostConstruct
    private void init(){
        testConfig.printProperties();
    }
}
@ConfigurationProperties("testconfig")
public class TestConfigurationProperties {
    private String first;
    private String second;
    private String third;
    private String fourth;
    private String fifth;
    private String sixth;
    private String seventh;
    private String eightth;
    
    //getters and setters

这样就可以通过 Bean 来使用。

2. 通过 @Value 使用

通过注解 @Value("${testconfig.first}") 可以给变量赋值成 配置 testconfig.first 的信息。

@Component
public class TestValue {

    @Value("${testconfig.first}")
    private String first;

    @Value("${testconfig.second}")
    private String second;

    @Value("${testconfig.third}")
    private String third;

    @Value("${testconfig.fourth}")
    private String fourth;

    @Value("${testconfig.fifth}")
    private String fifth;

    @Value("${testconfig.sixth}")
    private String sixth;

    @Value("${testconfig.seventh}")
    private String seventh;

    @Value("${testconfig.eightth}")
    private String eightth;

    public String getFirst() {
        return first;
    }

    public void setFirst(String first) {
        this.first = first;
    }

    public String getSecond() {
        return second;
    }

    public void setSecond(String second) {
        this.second = second;
    }

    public String getThird() {
        return third;
    }

    public void setThird(String third) {
        this.third = third;
    }

    public String getFourth() {
        return fourth;
    }

    public void setFourth(String fourth) {
        this.fourth = fourth;
    }

    public String getFifth() {
        return fifth;
    }

    public void setFifth(String fifth) {
        this.fifth = fifth;
    }

    public String getSixth() {
        return sixth;
    }

    public void setSixth(String sixth) {
        this.sixth = sixth;
    }

    public String getSeventh() {
        return seventh;
    }

    public void setSeventh(String seventh) {
        this.seventh = seventh;
    }

    public String getEightth() {
        return eightth;
    }

    public void setEightth(String eightth) {
        this.eightth = eightth;
    }

    public void printProperties(){
        System.out.println("
test value:");
        System.out.println("first: " + first);
        System.out.println("second: " + second);
        System.out.println("third: " + third);
        System.out.println("fourth: " + fourth);
        System.out.println("fifth: " + fifth);
        System.out.println("sixth: " + sixth);
        System.out.println("seventh: " + seventh);
        System.out.println("eightth: " + eightth);
    }
}

输出为:

test value:
first: ./config/
second: ./config/yml
third: classpath/config/
fourth: classpath
fifth: ./config/
sixth: ./config/
seventh: ./config/
eightth: ./config/

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/74015.html

相关文章

  • Spring-Boot学习笔记

    摘要:学习笔记使用很容易创建一个独立运行运行内嵌容器准生产级别的基于框架的项目,使用你可以不用或者只需要很少的配置。异常消息如果这个错误是由异常引起的。错误发生时请求的路径。 Spring-Boot 1.5 学习笔记 使用Spring Boot很容易创建一个独立运行(运行jar,内嵌Servlet容器)、准生产级别的基于Spring框架的项目,使用Spring Boot你可以不用或者只需要很...

    curlyCheng 评论0 收藏0
  • 吐血整理 20 道 Spring Boot 面试题,我经常拿来面试别人!

    摘要:你如何理解中的可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,你可以一站式集成及其他技术,而不需要到处找示例代码和依赖包。如你想使用访问数据库,只要加入启动器依赖就能使用了。 面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望。 下面,我给大家总结...

    haoguo 评论0 收藏0
  • SpringBoot 入门简介

    摘要:这里使用的是数据库启动类上加上注解在启动类中添加对包扫描扫描多个包下的可以有以下几种方法扫描会自动加载相关配置,数据源就会自动注入到中,会自动注入到中,可以直接使用。有配置文件下的使用扫描多个包下的可以有以下几种方法扫描 Spring-Boot 学习笔记 1 Spring-Boot 介绍 1.1 什么是Spring-Boot Spring-Boot是由Pivotal团队提供的全新框架...

    chuyao 评论0 收藏0
  • 第二十八章:SpringBoot使用AutoConfiguration自定义Starter

    摘要:代码如下所示自定义业务实现恒宇少年码云消息内容是否显示消息内容,我们内的代码比较简单,根据属性参数进行返回格式化后的字符串。 在我们学习SpringBoot时都已经了解到starter是SpringBoot的核心组成部分,SpringBoot为我们提供了尽可能完善的封装,提供了一系列的自动化配置的starter插件,我们在使用spring-boot-starter-web时只需要在po...

    fasss 评论0 收藏0
  • Spring Boot [组件学习-Spring]

    摘要:框架最初是由编写的,并且年月首次在许可下发布。在一个方法执行之后,只有在方法退出抛出异常时,才能执行通知在建议方法调用之前和之后,执行通知。方法执行之后,不考虑其结果,执行通知。 导读: 在上篇文章的结尾提到了Spring Boot 提供了一系列的框架整合(Starter POMs)帮助我们提升开发效率,但是这并不意味着我们不需要学习这些框架,反而更需要去学习,通过学习这些框架可以使...

    raoyi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<