如何与 3rd-party @ConfigurationProperties @Bean 一起工作?
How work with 3rd-party @ConfigurationProperties @Bean?
我使用 IntellijIdea 和 gradle。 Gradle 配置:
...
apply plugin: 'propdeps'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-maven'
buildscript {
repositories {
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies {
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
}
}
compileJava.dependsOn(processResources)
dependencies {
...
optional group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.4.0.RELEASE'
}
好的,为了创建我自己的属性,我需要:
@Component
@ConfigurationProperties("own.prefix")
@Data
public class TestProps {
public String field;
}
@Configuration
@EnableConfigurationProperties(TestProps.class)
public class AppConf {}
在我重建项目 spring-boot-configuration-processor 后生成新的元信息,所以在 application.properties 我可以使用 own.prefix.field= 和 Spring 看它。
但是我应该如何处理第 3 方配置 class?
文档 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 说:
As well as using @ConfigurationProperties
to annotate a class, you
can also use it on @Bean
methods. This can be particularly useful
when you want to bind properties to third-party components that are
outside of your control.
To configure a bean from the Environment properties, add
@ConfigurationProperties
to its bean registration:
@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
...
}
Any property defined with the foo prefix will be mapped onto that
FooComponent bean in a similar manner as the ConnectionProperties
example above.
好的。我们试试吧。例如,我在 gide (https://spring.io/guides/tutorials/spring-boot-oauth2/):
中声明了 bean
@Configuration
@EnableOAuth2Sso
public class SocialConfig {
@Bean
@ConfigurationProperties("facebook.client")
OAuth2ProtectedResourceDetails facebook() {
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("facebook.resource")
ResourceServerProperties facebookResource() {
return new ResourceServerProperties();
}
}
但是在重建项目后 属性 facebook.client 和 facebook.resource 不存在于我的 application.properties.
我也尝试将 SocialConfig.class 添加到
@Configuration
@EnableConfigurationProperties(SocialConfig.class)
public class AppConf {}
重建后还是不行。
像这样:
@Configuration
@EnableConfigurationProperties
public class AppConf {}
还是一样。
我做错了什么?
也对不起我的英语:)
您做错的是 @Configuration
class 中的方法不是 public。只需将 public
添加到 facebook
和 facebookResource
就可以了。我刚刚完善了 c4cb8317 and I've submitted a PR to fix the tutorial 您用作基础的文档。
此外,生成的元数据大部分为空,因为嵌套对象未使用 @NestedConfigurationProperty
标记。我已提交 another pull request 来解决这个问题。
我使用 IntellijIdea 和 gradle。 Gradle 配置:
...
apply plugin: 'propdeps'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-maven'
buildscript {
repositories {
maven { url 'http://repo.spring.io/plugins-release' }
}
dependencies {
classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.7'
}
}
compileJava.dependsOn(processResources)
dependencies {
...
optional group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.4.0.RELEASE'
}
好的,为了创建我自己的属性,我需要:
@Component
@ConfigurationProperties("own.prefix")
@Data
public class TestProps {
public String field;
}
@Configuration
@EnableConfigurationProperties(TestProps.class)
public class AppConf {}
在我重建项目 spring-boot-configuration-processor 后生成新的元信息,所以在 application.properties 我可以使用 own.prefix.field= 和 Spring 看它。
但是我应该如何处理第 3 方配置 class? 文档 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 说:
As well as using
@ConfigurationProperties
to annotate a class, you can also use it on@Bean
methods. This can be particularly useful when you want to bind properties to third-party components that are outside of your control.To configure a bean from the Environment properties, add
@ConfigurationProperties
to its bean registration:@ConfigurationProperties(prefix = "foo") @Bean public FooComponent fooComponent() { ... }
Any property defined with the foo prefix will be mapped onto that FooComponent bean in a similar manner as the ConnectionProperties example above.
好的。我们试试吧。例如,我在 gide (https://spring.io/guides/tutorials/spring-boot-oauth2/):
中声明了 bean@Configuration
@EnableOAuth2Sso
public class SocialConfig {
@Bean
@ConfigurationProperties("facebook.client")
OAuth2ProtectedResourceDetails facebook() {
return new AuthorizationCodeResourceDetails();
}
@Bean
@ConfigurationProperties("facebook.resource")
ResourceServerProperties facebookResource() {
return new ResourceServerProperties();
}
}
但是在重建项目后 属性 facebook.client 和 facebook.resource 不存在于我的 application.properties.
我也尝试将 SocialConfig.class 添加到
@Configuration
@EnableConfigurationProperties(SocialConfig.class)
public class AppConf {}
重建后还是不行。 像这样:
@Configuration
@EnableConfigurationProperties
public class AppConf {}
还是一样。
我做错了什么? 也对不起我的英语:)
您做错的是 @Configuration
class 中的方法不是 public。只需将 public
添加到 facebook
和 facebookResource
就可以了。我刚刚完善了 c4cb8317 and I've submitted a PR to fix the tutorial 您用作基础的文档。
此外,生成的元数据大部分为空,因为嵌套对象未使用 @NestedConfigurationProperty
标记。我已提交 another pull request 来解决这个问题。