【问题标题】:Accessing property file in spring boot在 Spring Boot 中访问属性文件
【发布时间】:2016-10-22 10:10:31
【问题描述】:

我正在创建一个简单的 Spring Boot 应用程序,我试图在其中访问外部 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。我无法从属性文件中获取值。

任何帮助将不胜感激......

【问题讨论】:

    标签: spring-boot properties-file


    【解决方案1】:

    src/main/resources 中的config.properties 文件还可以,但为什么要初始化:

    XmlOperation xmlOperation = new XmlOperation();

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

    基本上我会将XmlOperation 设为spring @Component 并通过IoC 将此组件注入IndexController

    XmlOperation 中的public String readXml(InputStream is) 的行为类似于标准服务,我将创建属性 filepath 并使用 @Value 注释从配置文件 (config.properties) 中注入值。


    完整示例:

    @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;
    
        }
    }
    

    【讨论】:

    • 不是@Component,可以加@Service注解吗?如果没有,为什么?我是 spring 新手,所以理解文档并不难。
    • @NewBeeDeveloper 如果您想在 XmlOperation 中拥有业务方法,您可以将类注释为服务。组件是某种通用注解,声明类在 Spring 容器中进行管理。
    猜你喜欢
    • 2020-06-30
    • 2021-07-11
    • 2019-11-24
    • 2016-04-29
    • 2018-03-01
    • 2020-11-10
    • 2023-03-29
    • 2021-03-15
    • 2018-10-15
    相关资源
    最近更新 更多