Skip to content

@ConfigurationProperties should be mandatory #3460

Description

@snicoll

Let's consider this sample use case: https://gist.github.com/snicoll/b2b08f783014ed2c6f02

Note that it only has @EnableConfigurationProperties(FooProperties.class) and no @ConfigurationProperties on FooProperties. This is the output of that application with the following configuration

name=foobar
description=my-desc
2015-07-10 21:37:21.137  INFO 2418 --- [           main] demo.DemoApplication                     : Starting DemoApplication on cancer-2.local with PID 2418 (/Users/snicoll/Downloads/demo-cp/target/classes started by snicoll in /Users/snicoll/Downloads/demo-cp)
2015-07-10 21:37:21.170  INFO 2418 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@52f759d7: startup date [Fri Jul 10 21:37:21 CEST 2015]; root of context hierarchy
Name --> null
Description --> null
2015-07-10 21:37:21.726  INFO 2418 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-07-10 21:37:21.735  INFO 2418 --- [           main] demo.DemoApplication                     : Started DemoApplication in 0.785 seconds (JVM running for 1.087)
2015-07-10 21:37:21.736  INFO 2418 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@52f759d7: startup date [Fri Jul 10 21:37:21 CEST 2015]; root of context hierarchy
2015-07-10 21:37:21.736  INFO 2418 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

The binding actually does happen but after the @PostConstruct method is invoked. If I uncomment @ConfigurationProperties the output shows the foobar and my-desc values respectively.

I think that @ConfigurationProperties should be mandatory and the processor should fail if a class is referenced and is not (meta) annotated with @ConfigurationProperties. Note also that no meta-data will be generated for such file since the processor uses the (legitimate) presence of @ConfigurationProperties.

This also makes the behaviour more consistent: when target classes are processed as beans, the presence of @ConfigurationProperties triggers the processing.

I'd also argue that the prefix should be mandatory and non empty but that's maybe going a tad too far.

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions