草庐IT

configurationproperties

全部标签

spring - Kotlin 和 Spring Boot @ConfigurationProperties

如何使用Kotlin在SpringBoot中正确初始化ConfigurationProperties?目前我喜欢下面的例子:@ConfigurationProperties("app")classConfig{varfoo:String?=null}但它看起来很丑,实际上foo不是variable,foo是constantvalue应该在启动时初始化,以后不会改变。 最佳答案 使用新的SpringBoot2.2,您可以这样做:@ConstructorBinding@ConfigurationProperties(prefix="sw

Spring Boot微服务从yml文件中加载配置(使用@Value和@ConfigurationProperties)

记录:398场景:在SpringBoot的微服务中从application.yml等yml文件中加载自定义配置内容。使用@Value直接加载单个配置。使用@ConfigurationProperties注解把一个或者多个配置加载为Java对象。版本:JDK1.8,SpringBoot2.6.31.使用@Value注解加载配置使用注解@RestController、@Service、@Component等标记的类中,都可以使用@Value注解从yml文件中加载配置取值。1.1在yml文件配置hub:example:city-name:'杭州'1.2使用@Value取值@Value("${hub.

java - 如何使用 @ConfigurationProperties 和 @Autowired 测试类

我想测试依赖于@Autowired和@ConfigurationProperties加载的属性的应用程序的一小部分。我正在寻找一种仅加载所需属性而不总是加载整个ApplicationContext的解决方案。这里作为简化示例:@TestPropertySource(locations="/SettingsTest.properties")@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes={TestSettings.class,TestConfiguration.class})publicclassS

java - Spring Boot @ConfigurationProperties 不从环境中检索属性

我正在使用SpringBoot1.2.1并尝试创建一个带有验证的@ConfigurationPropertiesbean,如下所示:packagecom.sampleapp;importjava.net.URL;importjavax.validation.constraints.NotNull;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;@Component@ConfigurationPr

java - Spring Boot bean 以 @ConfigurationProperties 值为条件

SpringBoot使用@ConfigurationProperties注释为我们提供了类型化的配置对象。优点之一是在使用SpringBoot注释处理器时可以免费在IDE中完成属性名称。另一个是:验证。现在,我想让bean以属性的值为条件。实际上,我有两个接口(interface)实现,这个属性告诉我应该使用哪一个。我可以这样实现它:ImplementationA.java@Component@ConditionalOnProperty(name="foo.bar",havingValue="a")publicclassImplementationAimplementsSomeInte

Spring 启动 : read list from yaml using @Value or @ConfigurationProperties

我想从yaml文件(application.yml)中读取主机列表,该文件如下所示:cors:hosts:allow:-http://foo1/-http://foo2/-http://foo3/(示例1)我使用的类定义了这样的值:@Value("${cors.hosts.allow}")ListallowedHosts;但由于Spring提示,读取失败:java.lang.IllegalArgumentException:Couldnotresolveplaceholder'cors.hosts.allow'instringvalue"${cors.hosts.allow}"当我像这

java - 在 `@ConfigurationProperties` 方法上使用 `@Bean` 注释

有人能给出如何在@Bean方法上直接使用@ConfigurationProperties注释的MWE吗?我已经看到了无数在类定义中使用它的例子——但还没有关于@Bean方法的例子。引用documentation:将此添加到类定义或@Bean方法@Target(value={TYPE,METHOD})所以,我认为也有可能和预期用途-但不幸的是我无法弄清楚。 最佳答案 spring.datasource.url=[url]spring.datasource.username=[username]spring.datasource.pas

java - 如果 bean 已经用 @ConfigurationProperties 注释,@EnableConfigurationProperties 有什么区别?

TheSpringBootdocumentation说要使用@ConfigurationProperties注释Youalsoneedtolistthepropertiesclassestoregisterinthe@EnableConfigurationPropertiesannotation,asshowninthefollowingexample:并给出以下代码:@Configuration@EnableConfigurationProperties(AcmeProperties.class)publicclassMyConfiguration{}但在下一段中说:Evenifth

java - @ConfigurationProperties 前缀不起作用

.yml文件cassandra:keyspaceApp:junitsolr:keyspaceApp:xyzbean@Component@ConfigurationProperties(prefix="cassandra")publicclassCassandraClientNew{@Value("${keyspaceApp:@null}")privateStringkeyspaceApp;主方法文件@EnableAutoConfiguration@ComponentScan@PropertySource("application.yml")publicclassCommonDataAp

java - 使用 ConfigurationProperties 以通用方式填充 Map

我想知道,是否有一种通用方法可以用您只知道前缀的属性填充map。假设有一堆类似的属性namespace.prop1=value1namespace.prop2=value2namespace.iDontKnowThisNameAtCompileTime=anothervalue我想要一个通用的方法来在map中填充这个属性,比如@Component@ConfigurationProperties("namespace")publicclassMyGenericProps{privateMappropmap=newHashMap();//setterandgetterforpropmapo