【问题标题】:user.dir not resolved properly in Intellij IDEAuser.dir 在 Intellij IDEA 中未正确解析
【发布时间】:2014-07-02 03:43:30
【问题描述】:

我在 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

【问题讨论】:

  • 启用调试(或跟踪)日志以查看user.dir 是如何解决的。我怀疑user.dir 属性在服务器启动时被覆盖,并且它指向应用程序部署/读取的目录。
  • 调试显示:Found key 'installationDirectory' in [applicationConfig: [classpath:/application.properties]] with type [String] and value '/Users/wdb/Library/Caches/IntelliJIdea13/compile-server/install'
  • 是的,但它也应该告诉你一些关于user.dir 属性的信息。可能在加载application.properties时。
  • AFAIK 无法从 JVM 中设置“user.dir”,因此您的进程未在您期望的目录中启动。也许使用 IDEA 的人可以帮助您找到正确的设置(Eclipse 开箱即用肯定会按照您的预期运行)。
  • 设置IDEA Run Config添加参数-Duser.dir=specificdirectory怎么样?

标签: java spring maven intellij-idea spring-boot


【解决方案1】:

我发现了问题。我正在使用 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 将要替代的东西。如果我查看target/classes 中的application.properties,它确实被替换了。

要修复,我需要执行以下操作:

首先告诉 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

在此之后,一切正常。

【讨论】:

    猜你喜欢
    • 2013-06-17
    • 2013-03-28
    • 2021-12-29
    • 2016-05-18
    • 2013-01-30
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多