Is it expected that configuration properties classes should be usable within @Configuration
classes.
Environment
- Spring Boot 2.0.2
- java version "10.0.1" 2018-04-17 Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10), Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
- OSX 10.13.4
My Code
application-local.properties:
s3.bucketName=test-bucket
Configuration Properties
@Validated
@ConfigurationProperties(prefix = "s3")
public class S3ConfigurationProperties {
@NotBlank
private String bucketName;
public String getBucketName() {
return bucketName;
}
public void setBucketName(final String bucketName) {
this.bucketName = bucketName;
}
}
Configuration Class
@Configuration
@Profile("local")
@EnableConfigurationProperties(S3ConfigurationProperties.class)
public class LocalS3Configuration {
@Autowired
private S3ConfigurationProperties properties;
@Value("${s3.bucketName}")
private String bucket;
@Bean(destroyMethod = "shutdown")
public AmazonS3 amazonS3(@Value("${local.s3.endpoint}") final String s3Endpoint, @Value("${s3.bucketName}") final String bucketName) {
// use properties...
final String bucketInjectedToMethod = bucketName; // works
final String bucketInjectedViaProperties = properties.getBucketName(); // null
final String bucketInjectedInClass = bucket; // null
}
}
Observed Behaviour
If I inject the S3ConfigurationProperties
as a field to the configuration class or an argument to the amazonS3
method the instance is non-null
, but the bucketName
property within it is null
.
Injecting the string to the class via @Value
is also null
.
The only way I can get it to work is to use the method argument annotated as @Value
with a string.
Is this expected behaviour or possibly a bug?