如何从maven依赖项的属性文件中获取位置?

时间:2015-05-11 19:39:41

标签: java spring maven configuration dependency-properties

人们,你知道是否有可能从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行工作吗?在那种情况下,你知道这样做的方法是什么?提前谢谢!

1 个答案:

答案 0 :(得分: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;
}

OR

@Component
class MyClass {
  @Resource(name="myProperties")
  private Properties myProperties;

  @PostConstruct
  public void init() {
    // do whatever you need with properties
  }
}