2

I use IntellijIdea and gradle. Gradle config:

...
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'
}

Ok, for creating my own properties i need:

@Component
@ConfigurationProperties("own.prefix")
@Data
public class TestProps {
    public String field;
}

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

And after i rebuild project spring-boot-configuration-processor genereate new META-INFO, so in application.properties i can use own.prefix.field= and Spring see it.

But what should i do with 3rd party configuration class? Docs http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html say:

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.

Ok. Lets try. For example I declare bean like in gide (https://spring.io/guides/tutorials/spring-boot-oauth2/):

@Configuration
@EnableOAuth2Sso
public class SocialConfig {

    @Bean
    @ConfigurationProperties("facebook.client")
    OAuth2ProtectedResourceDetails facebook() {
        return new AuthorizationCodeResourceDetails();
    }

    @Bean
    @ConfigurationProperties("facebook.resource")
    ResourceServerProperties facebookResource() {
        return new ResourceServerProperties();
    }
}

But after rebuilding project property facebook.client and facebook.resource do not exist in my application.properties.

Also i tried add SocialConfig.class to

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

After rebuild it still not work. And like this:

@Configuration
@EnableConfigurationProperties
public class AppConf {}

Still the same.

What am I doing wrong? Also sorry for my English :)

DamienMiheev
  • 998
  • 8
  • 31
  • What do you mean by "do not exist in my application.properties"? Are you saying that you don't have them as auto-complete values? You shouldn't rely on the auto-complete values as meaning that you can or can't add those properties to the configuration. The auto-complete is a best effort to reduce the memory of the developer on the naming of each property. Is auto-complete capabilities the only concern you have or is there another issue you are trying to resolve and hoped that auto-complete was the solution? – Shawn Clark Aug 28 '16 at 07:29
  • Yes, auto-complete in .properties is the main idea of question. Why does in first case it work grate, but in another my build.gradle looks like a yellow banana. I know i can hide notification but i want to implement autocomplete like in first case. Is it way to do that? – DamienMiheev Aug 28 '16 at 09:00

1 Answers1

2

What you're doing wrong is that the methods in your @Configuration class are not public. Just add public to facebook and facebookResource and you'll be fine. I've just polished that doc in c4cb8317 and I've submitted a PR to fix the tutorial you were using as a base.

Also the generated metadata were mostly empty since nested objects were not flagged with @NestedConfigurationProperty. I've submitted another pull request to fix that.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • Hm, as i know bean should not been private, so public is redundant, like in service interface. I am right, are not I? – DamienMiheev Aug 28 '16 at 09:06
  • No public is not redundant. – Stephane Nicoll Aug 28 '16 at 09:07
  • As i see you are contributor of this guide. So take a look on this case please: When i use public OAuth2ProtectedResourceDetails facebook(), configuration-processor generate only one property: facebook.client.scope[]. I do not know why, but if i change OAuth2ProtectedResourceDetails -> AuthorizationCodeResourceDetails i see all properties in application.properties. – DamienMiheev Aug 28 '16 at 10:23
  • Also idea move OAuth2ProtectedResourceDetails and ResourceServerProperties to one class does not work for me. processor generate spring-configuration-metadata.json like { "groups": [{ "name": "facebook", "type": "com.deliveryteam.delivery.configuration.OAuth2Client", "sourceType": "com.deliveryteam.delivery.configuration.SocialConfig", "sourceMethod": "facebook()" }], "properties": [], "hints": [] } So i have not any properties from inside classes in my application.properties. How to fix it? Thanks a lot. – DamienMiheev Aug 28 '16 at 10:56
  • Damien, it would be nice if you could focus on one problem. Answering to questions in comment is far from ideal, just create another thread. The answer is that the guide was wrong so I fixed it again: https://github.com/spring-guides/tut-spring-boot-oauth2/pull/36 – Stephane Nicoll Aug 29 '16 at 09:53
  • Yep, but problem was dropped step by step, so i think it was a part of one main question. Thanks a lot! – DamienMiheev Aug 29 '16 at 10:41