我在Spring Boot中使用了@Value
注释,但似乎并没有像我期望的那样完全正常工作。
在我的@Configuration
文件中:
@Value("${installationDirectory}")
private File m_installationDirectory;
在我的application.properties
:
installationDirectory=${user.dir}/install
启动:
public static void main( String[] args ) throws IOException
{
logger.info( "Starting application" );
logger.info( "Java version: {}", System.getProperty( "java.version" ) );
logger.info( "Java home : {}", System.getProperty( "java.home" ) );
logger.info( "Operation System: {} {} ({})", System.getProperty( "os.name" ), System.getProperty( "os.version" ), System.getProperty( "os.arch" ) );
logger.info( "Working dir : {}", System.getProperty( "user.dir" ) );
SpringApplication springApplication = new SpringApplication( Main.class );
springApplication.setShowBanner( false );
ConfigurableApplicationContext context = springApplication.run( args );
}
开始时的输出:
2014-05-14 09:36:05 INFO [main] Main - Starting application
2014-05-14 09:36:05 INFO [main] Main - Java version: 1.7.0_55
2014-05-14 09:36:05 INFO [main] Main - Java home : /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre
2014-05-14 09:36:05 INFO [main] Main - Operation System: Mac OS X 10.9.2 (x86_64)
2014-05-14 09:36:05 INFO [main] Main - Working dir : /Users/wdb/Work/netty-test
2014-05-14 09:36:05 INFO [main] Main - Starting Main on bruk-00007-l.zone2.flir.net with PID 98296 (/Users/wdb/Work/netty-test/flux-server/flux-server-application/target/classes started by wdb)
2014-05-14 09:36:05 DEBUG [main] Main - Running with Spring Boot v1.0.1.RELEASE, Spring v4.0.3.RELEASE
2014-05-14 09:36:08 INFO [main] LoggingToFileMessageRepositoryDecorator - Storing messages in /Users/wdb/Library/Caches/IntelliJIdea13/compile-server/install/messages
2014-05-14 09:36:08 INFO [main] OnDiskSingleJvmImageRepository - Storing images in folder /Users/wdb/Library/Caches/IntelliJIdea13/compile-server/install/images
2014-05-14 09:36:08 INFO [main] TrafficDataIntegratorsManagerImpl - Created 3 integrators for 1 sources in 1 ms
2014-05-14 09:36:09 INFO [main] Main - Started Main in 3.928 seconds (JVM running for 4.428)
请注意,如果我打印它,系统属性user.dir
将指向/Users/wdb/Work/netty-test
。但是,将installationDirectory
值注入Spring bean的路径中,路径似乎是/Users/wdb/Library/Caches/IntelliJIdea13/compile-server/install/
而不是预期的/Users/wdb/Work/netty-test/install
请注意,我在IntelliJ 13.1.2中运行,并且在我的运行配置中将“工作目录”设置为/Users/wdb/Work/netty-test
。
答案 0 :(得分:3)
我发现了这个问题。我正和Maven一起建设。 spring-boot-starter-parent
默认情况下会为application.properties
启用资源过滤:
<!-- Turn on filtering by default for application properties -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application.yml</include>
<include>**/application.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/application.yml</exclude>
<exclude>**/application.properties</exclude>
</excludes>
</resource>
</resources>
似乎${user.dir}
也是Maven将取代的东西。如果我查看application.properties
中的target/classes
,则确实已被替换。
要修复,我需要执行以下操作:
首先告诉Maven你想要escape filtering:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<escapeString>\</escapeString>
</configuration>
</plugin>
然后将application.properties
文件更改为:
installationDirectory=\${user.dir}/install
在此之后,一切正常。