在Spring Boot中使用 @ConfigurationProperties 注解

Spring Boot可使用注解的方式将自定义的properties(yml)文件映射到实体bean中,yml文件内容如下:

system:
  filePath: systemFilePath
  photoPath: E:/

 实体类中使用@ConfigurationProperties(prefix = "system") 注解,prefix = "system"  用来选择哪个属性的prefix名字来绑定,即对应yml文件中的system属性。

package com.touchspring.isite.base.config;

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

@Component
@ConfigurationProperties(prefix = "system")
public class SystemConfig {
    private String filePath;

    private String photoPath;

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }


    public String getPhotoPath() {
        return photoPath;
    }

    public void setPhotoPath(String photoPath) {
        this.photoPath = photoPath;
    }
}

 其他地方如何使用定义好的prefix属性,通过@Autowired  注入bean,即可调用yml文件定义的prefix属性。

package com.touchspring.isite.controller;

import com.touchspring.core.mvc.ui.ResultData;
import com.touchspring.isite.base.config.SystemConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {
    @Autowired
    private SystemConfig systemConfig;

    @GetMapping("prefix")
    public ResultData getSystemConfigPrefix(){
        String prefix = systemConfig.getFilePath();
        System.out.println("prefix:---"+ prefix);  //    prefix:prefix:--- system.filePath
        return ResultData.ok().putDataValue("prefix",prefix);
    }
}
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,可以使用@ConfigurationProperties注解来将配置文件属性值与Java对象进行绑定。这样可以方便地管理应用程序的配置属性。 下面是使用@ConfigurationProperties注解的步骤: 1. 创建一个Java类,并在类上添加@ConfigurationProperties注解。该注解的value属性指定了配置文件的前缀,prefix属性指定了配置文件属性前缀。例如,如果配置文件属性为myconfig.name,那么可以将value属性设置为"myconfig"。 ```java @ConfigurationProperties(prefix = "myconfig") public class MyConfigProperties { private String name; // getter和setter方法 } ``` 2. 在配置文件(application.properties或application.yml)定义属性值。例如,在application.properties添加以下内容: ``` myconfig.name=bj ``` 3. 在Spring Boot应用程序的主类使用@EnableConfigurationProperties注解来启用@ConfigurationProperties注解。将@ConfigurationProperties注解的类作为参数传递给@EnableConfigurationProperties注解。 ```java @SpringBootApplication @EnableConfigurationProperties(MyConfigProperties.class) public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 现在,可以在其他组件注入使用@ConfigurationProperties注解的类,并使用属性值。例如,在一个Service类注入MyConfigProperties类: ```java @Service public class MyService { private final MyConfigProperties configProperties; public MyService(MyConfigProperties configProperties) { this.configProperties = configProperties; } public void printName() { System.out.println("Name: " + configProperties.getName()); } } ``` 这样,就可以在应用程序使用@ConfigurationProperties注解来管理配置属性了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值