预加载资源文件需要先启用功能:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> 启用预加载,默认false </resource> </resources> </build>
在src/main/resources里,新增一个测试用的application.PRoperties文件
# application.propertiesapplication.name=${env.PATH}application.name=${settings.offline}application.name=${java.home}application.version=${pom.name}application.version=${project.version}
先看普通效果,运行cmd
mvn process-resources
被放到target/classes里
# application.propertiesapplication.name=C://ProgramData//Oracle//Java//javapath;C://Windows//system32;C://Windows;C://Windows//System32//Wbem;C://Windows//System32//WindowsPowerShell//v1.0//;F://MySQL//MySQL Server 5.1//bin;D://Program Files//TortoiseSVN//bin;D://Program Files//TortoiseGit//bin;d://Program Files (x86)//Git//cmd;F://apache-maven-3.2.3//bin;F://Java//jdk1.8.0_25//bin;F://HashiCorp//Vagrant//bin;F://Tomcat 7.0//binapplication.name=falseapplication.name=F://Java//jdk1.8.0_25//jreapplication.version=my-appapplication.version=1.0-SNAPSHOT
现在新建个src/main/filters/filter.properties,用于预加载:
# filter.propertiesmy.filter.value=hello!
更改pom.xml:
<build> <filters>指明预加载资源 <filter>src/main/filters/filter.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
这时候就可以在application.properties中直接引用了:
message=${my.filter.value}
就会变成:
message=Hello!
这样就实现了资源文件间的属性引用,而无需声明二者的关联(Spring?)
新闻热点
疑难解答