在 spring 引导中访问 属性 文件

Accessing property file in spring boot

我正在创建一个简单的 spring 引导应用程序,我试图在其中访问外部 config.properties 文件。

IndexController.java

@Controller
public class IndexController {

    XmlOperation xmlOperation = new XmlOperation();

    @RequestMapping("/")
    public String greeting() {
        return "greeting";
    }

    @RequestMapping(params = "btnOpen", method = RequestMethod.POST)
    public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
        try {
            InputStream is = file.getInputStream();
            model.addAttribute("fileContent", xmlOperation.readXml(is));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return "greeting";
    }
}

XmlOperation.java

@PropertySource("classpath:config.properties")
public class XmlOperation {

    @Autowired
    Environment env;

    public String readXml(InputStream is) throws IOException {
        System.out.println(env.getProperty("filepath"));
        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer, StandardCharsets.UTF_8);
        String fileContent = writer.toString();
        return fileContent;

    }

config.properties 文件位于 src/main/resources。我无法从 属性 文件中获取值。

如有任何帮助,我们将不胜感激...

config.properties文件在src/main/resources是可以的但是你为什么要初始化:

XmlOperation xmlOperation = new XmlOperation();

IndexController?而且我也不确定 XmlOperation 是否是 spring 组件(问题中你只有 @PropertySource over XmlOperation)。

基本上我会将 XmlOperation 作为 spring @Component 并使用 IoC 将此组件注入 IndexController

public String readXml(InputStream is) in XmlOperation 的行为类似于标准服务,我会创建 属性 filepath 并使用 [= 从配置文件 (config.properties) 中注入值25=]注解.


完整示例:

@Controller
public class IndexController {

    @Autowired
    private XmlOperation xmlOperation;

    @RequestMapping("/")
    public String greeting() {
        return "greeting";
    }

    @RequestMapping(params = "btnOpen", method = RequestMethod.POST)
    public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
        try {
            InputStream is = file.getInputStream();
            model.addAttribute("fileContent", xmlOperation.readXml(is));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return "greeting";
    }
}

@Component
@PropertySource("classpath:config.properties")
public class XmlOperation {

    // use this when XmlOperation is @Configuration bean and you want to create @Bean-s e.g
    // @Autowired
    // Environment env;

    // for your case inject property like this
    @Value("${filepath}")
    private String filepath;

    public String readXml(InputStream is) throws IOException {

        // dont use this
        //System.out.println(env.getProperty("filepath"));

        // rather this
        System.out.println(filepath);

        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer, StandardCharsets.UTF_8);
        String fileContent = writer.toString();
        return fileContent;

    }
}