【问题标题】:Spring boot: Read a file PATH from "application.properties"Spring boot:从“application.properties”读取文件PATH
【发布时间】:2015-04-30 05:52:34
【问题描述】:

我正在使用 Spring Boot,我想从 resources/application.properties 文件中读取文件路径并将其值设置为 Java 类中的字符串:

resources/application.properties

mypath=dir/file.ext

resources/application-context.xml

<context:property-placeholder location="classpath:application.properties" />

MyJavaClass.java

@Component
public class MyJavaClass{ 

    @Value("${mypath}") 
    String mypath;

    public void printme(){
        System.err.println(mypath);
    }
}

当我尝试打印字符串时,它总是打印“null”。我究竟做错了什么?谢谢。

【问题讨论】:

    标签: java xml spring properties


    【解决方案1】:

    资源文件夹在类路径中吗?如果不这样尝试

    <bean id="propertyLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
                <value>classpath:application.properties</value>
        </property>
    </bean>
    

    我假设您的属性文件与它所引用的配置文件位于同一位置。

    【讨论】:

    • 是的,资源文件夹在类路径中。
    【解决方案2】:

    最后我解决了这个问题。我试图使用“主”线程(从主函数执行代码的线程)中的值注释。

    例如,如果我从服务的代码中获取值,它会很好用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-15
      • 2018-01-07
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      相关资源
      最近更新 更多