Java Records are not working with ConfigurationProperties annotation
Asked Answered
P

1

7

I'm using @ConfigurationProperties annotation to auto-config my properties. Before my config class was working fine, but I tried to achieve the same thing with records and just fails.

I was following this SO answer, but in my case it didn't work: https://mcmap.net/q/302690/-how-to-use-configurationproperties-with-records

In my application.properties there is a property which is cache.validity=200.

This is the code

@Component
@ConfigurationProperties("cache")
public record MyConfig(int validity) {

    @ConstructorBinding
    public MyConfig(int validity) {
        this.validity= Optional.ofNullable(validity).orElse(0);
    }
}

When I try to start my application I get the following error:

Description:

Parameter 0 of constructor in ...MyConfig required a bean of type 'int' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'int' in your configuration.

What is the proper way to make this work? Thanks in advance.

Edit: My pom.xml:

    <properties>
        <java.version>17</java.version>
        <spring.boot.version>2.5.4</spring.boot.version>
        <spring.version>5.3.9</spring.version>
    </properties>

My application.properties:

server.port=8060
cache.validity=0

... (DB Drivers/Security Configs)
Performing answered 23/11, 2021 at 11:24 Comment(0)
B
6

Ok I figured it out :)

Furthermore, it's important to emphasize that to use the constructor binding, we need to explicitly enable our configuration class either with @EnableConfigurationProperties or with @ConfigurationPropertiesScan.

Working example below with SpringBoot 2.5.4 and Java 17:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@SpringBootApplication
@ConfigurationPropertiesScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

package com.example.demo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@ConfigurationProperties("cache")
@ConstructorBinding
public record ExampleRecordConfig(int validity) {
}

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ServiceWhichUsesConfig {

    @Autowired
    private ExampleRecordConfig exampleRecordConfig;


    public void justChecking(){
        System.out.println(exampleRecordConfig.validity());
    }

}

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private ServiceWhichUsesConfig serviceWhichUsesConfig;

    @Test
    void contextLoads() {
        serviceWhichUsesConfig.justChecking();
    }

}

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

What made it work: @ConfigurationPropertiesScan

=== Edited ===

Just for fun, to answer your question about default values. You could achieve it in such a way:

The cons for this is that you default in code, rather than configuration.

@ConfigurationProperties("cache")
@ConstructorBinding
public record ExampleRecordConfig(Integer validity) {

    public Integer validity(){  <-- notice the name..it cant be getValidity()
        if(validity != null)
            return validity;
        return 500;
    }
}

If you dont have cache.validity defined in your properties file at all, then it will return 500



For configuration file defaults, you will need to do it in a different way:

somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInProperitiesFile=5
server.port=8060
cache.validity=${somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInProperitiesFile:5000000}

Then it will print 5 because the first somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInPrope has a value.


server.port=8060
cache.validity=${somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInProperitiesFile:5000000}

Then it will print 5000000 which is the default, if no somePropertyEitherFromEnvironmentOrAlreadyDefinedUpInPrope is defined;

Brandabrandais answered 23/11, 2021 at 12:35 Comment(10)
Same error unfourtunetlyPerforming
What speingboot version are you using?Brandabrandais
And please show your properties fileBrandabrandais
I have added them. Maybe the Dependency Injection part fails?Performing
I am going to try and reproduce it locally, @Performing Keep you updatedBrandabrandais
@Performing I made it work, please see updated answerBrandabrandais
It's working fine, thank you for your effort :)Performing
replied @Performing Anything is possible :)Brandabrandais
please tell me if it makes sense @Performing otherwise I can rewrite it so it is easier to understandBrandabrandais
Yes it makes perfect sense, thank you very much for the clear and easy work :)Performing

© 2022 - 2024 — McMap. All rights reserved.