给定一个 application.properties 文件:
myProperties.map.key1=value1
myProperties.map.key2=value2
myProperties.map.key with space=value3
Run Code Online (Sandbox Code Playgroud)
和属性类:
@ConfigurationProperties
public class MyProperties {
private Map<String, String> map;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我试图逃离类似key\ with\ space
或什至的空间,但两者仍然像地图中key\u0020with\u0020space
那样结束。keywithspace
请指出使用Spring application.properties添加带空格键映射的正确方法,谢谢。
我希望能够从属性文件中读取活动配置文件,以便可以在基于Spring MVC的Web应用程序中使用不同的配置文件配置不同的环境(dev,prod等).我知道可以通过JVM参数或系统属性设置活动配置文件.但我想通过属性文件来代替.关键是我不静态地知道活动配置文件,而是想从属性文件中读取它.看起来这是不可能的.例如,如果我在application.properties中有'spring.profiles.active = dev',并允许在override.properties中覆盖它,如下所示:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:/application.properties</value>
<value>file:/overrides.properties</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
该配置文件未在环境中被拾取.我想这是因为在bean初始化之前正在检查活动的配置文件,因此不尊重在属性文件中设置的属性.我看到的唯一另一个选项是实现一个ApplicationContextInitializer,它将按优先级顺序加载这些属性文件(如果存在则覆盖首先是override.properties,否则是application.properties)并在context.getEnvironment()中设置值.有没有更好的方法从属性文件中执行此操作?
根据 spring 文档,诸如 之类的字符串属性值10s
将被正确转换为java.time.Duration
.
事实上,这对我来说适用于主要的应用程序属性。但它在单元测试中失败了。
单元测试
@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = MyConfig.class)
@TestPropertySource("classpath:test.properties")
public class MyUnitTest {
@Autowired
MyConfig config;
...
}
Run Code Online (Sandbox Code Playgroud)
配置类
@ConfigurationProperties(prefix = "my")
@Component
public class MyConfig {
@Value("${my.failed-duration}")
public Duration myDuration;
...
}
Run Code Online (Sandbox Code Playgroud)
测试.属性
my.failed-duration=10s
Run Code Online (Sandbox Code Playgroud)
例外
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:350)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:355)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$7(ClassBasedTestDescriptor.java:350)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735) …
Run Code Online (Sandbox Code Playgroud) 我不确定我是否很好地了解Spring配置文件如何与yaml和属性文件一起使用。我试图混杂这两种类型的配置(这两个文件不共享任何配置),但是从yaml配置读取配置文件时遇到了问题。
我正在使用Spring 4.1.1
这是代码。这是context:property-placeholder配置:
<context:property-placeholder location="classpath:/job-config.properties" order="1"
ignore-unresolvable="true" ignore-resource-not-found="false"/>
<context:property-placeholder properties-ref="yamlProperties" order="2"
ignore-resource-not-found="false" ignore-unresolvable="true"/>
Run Code Online (Sandbox Code Playgroud)
其中yamlProperties是以下bean
<bean id="yamlProperties"
class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources"
value="file:${catalina.home}/properties/test.yml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
这是test.yml
spring:
profiles.default: default
---
spring:
profiles: default
db:
url: jdbc:oracle:thin:@##hostname##:##port##:##SID##
usr: ##USER##
pwd: ##PWD##
---
spring:
profiles: development
db:
url: jdbc:oracle:thin:@##hostname##:##port##:##SID_DEVELOPMENT##
usr: ##USER_DEVELOPMENT##
pwd: ##PWD_DEVELOPMENT##
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我尝试通过以下方式配置(通过xml)我的数据源时:
<property name="url" value="${db.url}"/>
<property name="username" value="${db.usr}"/>
<property name="password" value="${db.pwd}"/>
Run Code Online (Sandbox Code Playgroud)
Spring始终使用YAML文件中的最后一个配置,而忽略该配置文件。我试图通过web.xml中的contex-parameter传递活动配置文件,或者直接将其传递给JVM(我实现了一个实现EnvironmentAware接口的bean,以获取活动/默认配置文件,并且是正确的),但看起来一切都很好,但是,当尝试注入值将忽略配置文件。
我相信使用property-placeholder上下文(带有订单)可以获得一个Property-placeholder,它是PropertySourcesPlaceholderConfigurer的一个实例,因此可以访问Environment,但是我无法理解为什么配置文件被忽略并且spring获得了最后的yaml文件配置。
我在63.6节http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html中添加了对文档的引用(spring-boot)。
提前致谢
spring yaml spring-profiles spring-properties property-placeholder
我有一个application.properites文件,其中包含以下内容:
xxx.xxx = sandbox
xxx.sandbox = 123
xxx.production = 456
Run Code Online (Sandbox Code Playgroud)
我希望在xxx.xxx == sandbox的情况下映射到字符串值123,在xxx.xxx == production的情况下映射到456
...
public class temp {
@Value("${??????}")
private String token;
}
Run Code Online (Sandbox Code Playgroud)
是否有可能填补一个被煽动的条件?根据xxx.xxx将令牌映射到123或456?
我正在使用application.yml文件配置Spring Boot应用程序:
foo:
bar: foobar
foolist:
- bar: foobar1
baz: foobaz1
- bar: foobar1
baz: foobaz1
Run Code Online (Sandbox Code Playgroud)
我可以使用环境变量轻松设置foo.bar值,例如
export FOO_BAR=value
Run Code Online (Sandbox Code Playgroud)
如何设置foolist入口的值?FOOLIST[0]_BAR
不是有效的标识符,FOOLIST_0_BAR
因此不起作用。
我正在使用spring-boot-starter-parent
版本2.4.4
.
我正在尝试使用和不使用类型化配置进行验证@ConstructorBinding
。
这是一个经过验证并正确停止应用程序运行的类型化配置。它不使用@ConstructorBinding
,而是使用 getter/setter 样式:
@ConfigurationProperties("app")
@Validated
@Getter
@Setter
public static class AppProperties implements org.springframework.validation.Validator {
private int someProp = 10;
@Override
public boolean supports(Class<?> aClass) {
return aClass.isAssignableFrom(getClass());
}
@Override
public void validate(Object o, Errors errors) {
AppProperties appProperties = (AppProperties) o;
if (appProperties.getSomeProp() < 100) {
errors.rejectValue("someProp", "code", "Number should be greater than 100!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
网络应用程序已设置,@ConfigurationPropertiesScan
因此一切正常。运行时,正如预期的那样,我得到了这个:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'app' …
Run Code Online (Sandbox Code Playgroud) 我有一个方法对象。
我想Type
用泛型提取 return 并将其转换为 aClass
以便将此类信息传递到 Spring 中PropertyResolver
。
Type type = myMethod.getGenericReturnType();
Class<?> returnType = /* ??? */;
environment.getProperty(key, returnType);
Run Code Online (Sandbox Code Playgroud) 我在 Java 类中创建了这样的配置文件,
@Profile(value = "cache")
public class MapCache {
....
}
Run Code Online (Sandbox Code Playgroud)
使用spring.profiles.active时,这些配置文件不会被激活,但如果我使用spring.profiles.include配置文件工作正常。
我想通过在 application.properties 中添加的属性来激活配置文件
注意:应用程序在独立的jetty实例中运行。
任何提示都会很棒。
Spring属性可以从环境变量绑定:https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding 。环境变量
我尝试过各种形式的null
,NULL
但#{null}
最终总是failed to convert java.lang.String to java.time.Duration (caused by java.lang.IllegalArgumentException: 'null' is not a valid duration)
如何null
从环境变量中设置值?