首页 文章

从Spring Boot REST应用程序中的Application.properties读取属性

提问于
浏览
0

我试图从我的Spring Boot rest应用程序的application.properties(存在于src / main / resources中)访问自定义属性,并尝试从我的控制器访问该值

Spring启动应用程序类位置“src / main / java / com / myapp / FileReaderApp.java”

package com.myapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class FileReaderApp {

public static void main(String[] args) {
    SpringApplication.run(FileReaderApp.class, args);
    }
}

控制器类位置:“src / main / java / com / myapp / controller / FileReaderController.java”

@RestController
@RequestMapping("/counter-api")
public class FileReadController {

    @Value("${fileLocation}")
    static private String fileLocation;
    //do other actions 
   }

我发现我根本无法做到这一点,每次fileLocation都是null .

我的application.properties文件只有一个属性fileLocation .

在这种情况下,有没有人可以告诉我哪里出错了?在REST控制器类中访问应用程序属性值的最佳方法是什么?

1 回答

  • 1

    你正在注入一个 static 字段 . 这不受支持 .

相关问题