Skip to content

ConfigurationProperties annotation: external java class does not load configuration values #45371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davgia opened this issue May 6, 2025 · 1 comment
Labels
status: invalid An issue that we don't feel is valid

Comments

@davgia
Copy link

davgia commented May 6, 2025

Spring boot: 3.4.5
Java: 21 (tested on 17 too)
Sample: https://github.com/davgia/demo-service
Original issue: spring-cloud/spring-cloud-kubernetes#1915

I have a bean annotated with @ConfigurationProperties with a prefix. If the java class of the bean is declared in the same maven module the class contains the values from the configuration as expected (in this case with the values from the application.yml file). On the contrary, if the declared class is from another maven module the bean does not have the values from the configuration.

Here it is a snippet to better illustrate the structure:

@Configuration
public class ServiceConfig {
    @Bean
    @ConfigurationProperties(prefix = "demo.connections")
    public InternalBaseR2dbcConfig getInternalR2dbcConfig() {
        return new InternalBaseR2dbcConfig();
    }

    @Bean
    @ConfigurationProperties(prefix = "demo.connections")
    public BaseR2dbcConfig getExternalR2dbcConfig() {
        return new BaseR2dbcConfig();
    }
}

In the sample project I tried to declare the same class structure in the same and in a separate maven module. Both classes are used by beans declared in the same @configuration class and they load from the same configuration prefix. What happens is that the bean associated with the "internal" class does have the values from the configuration, while the bean from the "external" class does not.

Thanks!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 6, 2025
@wilkinsona
Copy link
Member

As noted in this section of the documentation, for constructor binding to work the code must be compiled with -parameters. Your service module is using spring-boot-starter-parent so it inherits the necessary compiler configuration. Your library module is not using spring-boot-starter-parent so it needs to configure the compiler itself. One way to do so is by using the maven.compiler.parameters property:

<maven.compiler.parameters>true</maven.compiler.parameters>

The app then successfully binds properties to both classes:

2025-05-06T11:33:14.754+01:00  INFO 89849 --- [demo-service] [           main] org.demo.service.config.ServiceConfig    : Internal: [{"test-res":{"uri":"r2dbc:sqlserver://sa@localhost:1433/test","connectTimeout":null,"database":null,"driver":null,"host":null,"lockWaitTimeout":null,"password":"test","port":null,"protocol":null,"ssl":null,"statementTimeout":null,"user":null}}]
2025-05-06T11:33:14.760+01:00  INFO 89849 --- [demo-service] [           main] org.demo.service.config.ServiceConfig    : External: [{"test-res":{"uri":"r2dbc:sqlserver://sa@localhost:1433/test","connectTimeout":null,"database":null,"driver":null,"host":null,"lockWaitTimeout":null,"password":"test","port":null,"protocol":null,"ssl":null,"statementTimeout":null,"user":null}}]

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale May 6, 2025
@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants