ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-user.dir在Intellij IDEA中无法正确解析

2019-11-21 19:13:53  阅读:719  来源: 互联网

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


我在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 /安装

请注意,我是从IntelliJ 13.1.2运行的,在运行配置中将“工作目录”设置为/ Users / wdb / Work / netty-test.

解决方法:

我发现了问题.我正在与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,的确被替换了.

要解决此问题,我需要执行以下操作:

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

之后,一切正常.

标签:spring-boot,maven,intellij-idea,spring,java
来源: https://codeday.me/bug/20191121/2054054.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有