将文件添加到 Spring Boot 的类路径
Adding a file to Spring Boot's classpath
我有 server.yml
文件,其中仅包含 Spring 框架 端口号、上下文根路径、应用程序名称等属性。
而且,我有一个 applicationContext.xml
具有以下内容:
<util:properties id="springProperties" location="classpath*:my.properties">
<context:property-placeholder location="classpath*:my.properties"
local-override="true" properties-ref="springProperties" />
my.properties
文件位于项目的 src/main/resources
目录中。
那么,我可以从我的 java 类 访问属性,例如:
@Autowired
@Qualifier("springProperties")
private Properties props;
public String getProperty(String key){
return props.getProperty(key);
}
or like `${my.prop}`
当我构建 war 和 运行 Spring 引导 (java -jar server.war
) 时,内部 my.properties
解析,一切都按预期工作。
但是,我想用外部 my.properties
覆盖该文件。
我读 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
我试过 运行 类似的东西:
java -jar server.jar --spring.config.location=classpath:/my.properties
或
java -jar server.jar --spring.config.location=my.properties
但是我唯一可以覆盖的属性来自我的 server.yml
。意思是,我可以覆盖端口号或应用程序名称。但内部 my.properties
永远不会受到影响。
我是不是做错了什么?我知道外部 my.property
应该在类路径中,然后它会覆盖内部 my.property
。但它永远不会发生。
您可以使用@PropertySource({ "classpath:override.properties" }) 从类路径添加额外文件,然后使用环境对象获取值或值的@Value 注释
@PropertySource({ "classpath:other.properties" })
@Configuration
public class Config{
@Autowired
private Environment env; //use env.getProperty("my.prop")
@Value("${my.prop}")
private String allowedpatterns;
}
@Component
public OthenClass{
@Autowired
//If this class is not component or Spring annotated class then get it from ApplicationContext
private Environment env; //use env.getProperty("my.prop")
@Value("${my.prop}")
private String allowedpatterns;
}
如果您正在使用Spring Boot below 代码可用于获取ApplicationContext
ConfigurableApplicationContext ctx = app.run(args);
Environment env = ctx.getBean(Environment.class);
我有 server.yml
文件,其中仅包含 Spring 框架 端口号、上下文根路径、应用程序名称等属性。
而且,我有一个 applicationContext.xml
具有以下内容:
<util:properties id="springProperties" location="classpath*:my.properties">
<context:property-placeholder location="classpath*:my.properties"
local-override="true" properties-ref="springProperties" />
my.properties
文件位于项目的 src/main/resources
目录中。
那么,我可以从我的 java 类 访问属性,例如:
@Autowired
@Qualifier("springProperties")
private Properties props;
public String getProperty(String key){
return props.getProperty(key);
}
or like `${my.prop}`
当我构建 war 和 运行 Spring 引导 (java -jar server.war
) 时,内部 my.properties
解析,一切都按预期工作。
但是,我想用外部 my.properties
覆盖该文件。
我读 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
我试过 运行 类似的东西:
java -jar server.jar --spring.config.location=classpath:/my.properties
或
java -jar server.jar --spring.config.location=my.properties
但是我唯一可以覆盖的属性来自我的 server.yml
。意思是,我可以覆盖端口号或应用程序名称。但内部 my.properties
永远不会受到影响。
我是不是做错了什么?我知道外部 my.property
应该在类路径中,然后它会覆盖内部 my.property
。但它永远不会发生。
您可以使用@PropertySource({ "classpath:override.properties" }) 从类路径添加额外文件,然后使用环境对象获取值或值的@Value 注释
@PropertySource({ "classpath:other.properties" })
@Configuration
public class Config{
@Autowired
private Environment env; //use env.getProperty("my.prop")
@Value("${my.prop}")
private String allowedpatterns;
}
@Component
public OthenClass{
@Autowired
//If this class is not component or Spring annotated class then get it from ApplicationContext
private Environment env; //use env.getProperty("my.prop")
@Value("${my.prop}")
private String allowedpatterns;
}
如果您正在使用Spring Boot below 代码可用于获取ApplicationContext
ConfigurableApplicationContext ctx = app.run(args);
Environment env = ctx.getBean(Environment.class);