大家,你们知道是否可以从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 行工作吗?在这种情况下,你知道该怎么做吗?提前致谢!
最佳答案
您无法从日志记录配置加载属性文件。相反,创建 配置文件中的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
}
}
关于java - 如何从maven依赖的属性文件中获取位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30176264/