0

My question was already asked by someone else and had an answer 11 years ago here:

How can I inject a property value into a Spring Bean which was configured using annotations?

That was 2008. It is now 2019 and my code still looks like this:

@Configuration
public class Config {

    private @Value("${region}") String region;

    private @Value("${bucket}") String bucket;

    @Bean
    AwsS3Template getAwsS3Template() {
        AwsS3Template s3 = new AwsS3Template();
        s3.setRegion(region);
        s3.setBucket(bucket);
        return s3;
    }

}

Is there now a way to do this fully declaratively?

I would like to write something like this:

@Configuration
public class Config {

    @Bean
    @AutoApplyPropertyValues /* pseudo-code, no such annotation exists */
    AwsS3Template getAwsS3Template() {
        return new AwsS3Template();
    }

}
Alex R
  • 11,364
  • 15
  • 100
  • 180
  • 2
    Have you looked at `@ConfigurationProperties` in Spring Boot? – Andy Wilkinson Mar 09 '19 at 23:50
  • Not applicable. `@ConfigurationProperties` has to be applied to the class that has the properties. Note the nuance: the class that has the properties here is a third-party class without any spring annotations. – Alex R Mar 10 '19 at 01:37
  • Please be a little less dismissive of those trying to help you. I understand the nuance. You can use `@ConfigurationProperties` on a `@Bean` method: https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-external-config-3rd-party-configuration – Andy Wilkinson Mar 10 '19 at 08:22
  • that worked! please post it as an answer @AndyWilkinson – Alex R Mar 10 '19 at 21:59

1 Answers1

0

from the comments

@Configuration
public class Config {

    @Bean
    @ConfigurationProperties
    AwsS3Template getAwsS3Template() {
        return new AwsS3Template();
    }

}
Alex R
  • 11,364
  • 15
  • 100
  • 180