【问题标题】:how to get location from maven dependency's properties file?如何从 Maven 依赖项的属性文件中获取位置?
【发布时间】:2015-07-22 11:09:14
【问题描述】:

各位,您知道是否可以从 maven 依赖项中获取属性文件?更准确地说,我使用的是 Spring 4,我的 application.properties 看起来像这样:

logging.level.org.springframework.web: INFO
logging.level.org.hibernate: ERROR
logging.level.com.example.movies: DEBUG

spring.config.location=example-backend-development-domain/src/main/resources/application.properties

最后一行不起作用。但是我想要指定这个 application.properties 文件位于 maven 依赖项内的资源文件夹内(顺便说一下,它与这个实际项目的父级相同)。 所以,这就是依赖:

    <dependency>
        <groupId>com.despegar.qc.example</groupId>
        <artifactId>example-backend-development-domain</artifactId>
    </dependency>

你知道这是否可能使 spring.config.location 行工作?在那种情况下,你知道该怎么做吗?提前致谢!

【问题讨论】:

    标签: java spring maven configuration dependency-properties


    【解决方案1】:

    您无法从日志记录配置中加载属性文件。相反,创建 配置文件中的bean标签并使用它。

    <bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="locations">
        <list>
          <value>classpath*:application.properties</value>
        </list>
      </property>
    </bean>
    

    你可以以任何一种方式使用它:

    @Component
    class MyClass {
      @Value("${propertyName}")
      private String myValue;
    }
    

    @Component
    class MyClass {
      @Resource(name="myProperties")
      private Properties myProperties;
    
      @PostConstruct
      public void init() {
        // do whatever you need with properties
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 2023-03-12
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多