java - 如何使用第 3 方 @ConfigurationProperties @Bean?

标签 java spring spring-boot configuration property-files

我使用 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 后,生成新的 META-INFO,因此在 application.properties 中我可以使用 own.prefix.field= 并且 Spring 会看到它。

但是我应该如何处理第 3 方配置类? 文档 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();
    }
}

但是重建项目属性后,我的 application.properties 中不存在 facebook.client 和 facebook.resource。

我还尝试将 SocialConfig.class 添加到

@Configuration
@EnableConfigurationProperties(SocialConfig.class)
public class AppConf {}

重建后仍然无法工作。 像这样:

@Configuration
@EnableConfigurationProperties
public class AppConf {}

还是一样。

我做错了什么? 也对不起我的英语:)

最佳答案

你做错的是你的@Configuration类中的方法不是公开的。只需将 public 添加到 facebookfacebookResource 即可。我刚刚在 c4cb8317 中完善了该文档我已经submitted a PR to fix the tutorial您用作基础。

此外,生成的元数据大部分为空,因为嵌套对象未使用 @NestedConfigurationProperty 进行标记。我已提交another pull request来解决这个问题。

关于java - 如何使用第 3 方 @ConfigurationProperties @Bean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39186670/

相关文章:

java - JTable setPreferredWidth() 用于显示不正确的列

java - Eclipse(Java 和 Hibernate)内容助手不工作

java - 事务内部的Spring Boot LazyInitializationException

java - Spring Data Neo4j starter 和 OGM SessionFactory 中 Cannot merge node using null property value for uuid 的原因是什么?

java - Aspose with RJB (Ruby Java Bridge) 不工作

spring - oauth/token 访问此资源需要完整的身份验证

java - 由于属性文件位置类型,应用程序或集成测试 Spring 上下文创建失败

java - 为什么我的 Spring 服务返回客户端请求的任何内容类型?

java - 捕获并增强LockedException和DisabledException

java - 为什么在对结果求和之前需要左移?