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

时间:2021-03-06 23:31:41

People,do you know if its possible to get a properties file from maven dependency? More preciselly, i am using Spring 4, and i have my application.properties looking like this:

人们,你知道是否可以从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

This last line is not working. But what i want is to specify that there is this application.properties file which is inside resources folder inside a maven dependency (which has, by the way, the same parent of this actual project). So, this is that dependency:

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

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

Do you know if this is even possible to make the spring.config.location line work? And in that case, do you know what would be the way to do that? Thanks in advance!

你知道这是否有可能使spring.config.location行工作?在那种情况下,你知道这样做的方法是什么?提前致谢!

1 个解决方案

#1


You can't load property files from logging configuration. Instead, create bean tag in configuration file and use it.

您无法从日志记录配置加载属性文件。而是在配置文件中创建bean标记并使用它。

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

And you can use it in either way:

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

@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
  }
}

#1


You can't load property files from logging configuration. Instead, create bean tag in configuration file and use it.

您无法从日志记录配置加载属性文件。而是在配置文件中创建bean标记并使用它。

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

And you can use it in either way:

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

@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
  }
}