mercedes-benz / sechub

SecHub provides a central API to test software with different security tools.
https://mercedes-benz.github.io/sechub/
MIT License
259 stars 58 forks source link

Epic: Use Configuration Properties instead of @Value (Spring) #3212

Open hamidonos opened 2 weeks ago

hamidonos commented 2 weeks ago

Situation

In Spring, you have the option to use @Value or @ConfigurationProperties for injecting configuration properties. While both approaches are valid, there are several reasons why you might prefer to use @ConfigurationProperties over @Value.

Sub issues

(Remark we collect sub issues via this issue and use it as an epic)

Reasons to prefer @ConfigurationProperties over @Value.

Group Related Properties:

With @ConfigurationProperties, you can group related properties into a single class, making your configuration more organized and easier to manage.

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private int timeout;

    // getters and setters
}

Easier Testing:

Configuration classes using @ConfigurationProperties can be instantiated and tested independently of the Spring context, which simplifies unit testing.

@SpringBootTest
public class AppPropertiesTest {
    @Autowired
    private AppProperties appProperties;

    @Test
    void testProperties() {
        assertEquals("MyApp", appProperties.getName());
        assertEquals(30, appProperties.getTimeout());
    }
}

Validation Support:

@ConfigurationProperties can be combined with JSR-303 (Bean Validation) annotations to validate configuration properties. This ensures that your application fails fast if required properties are missing or invalid.

@ConfigurationProperties(prefix = "app")
public class AppProperties {
    @NotNull
    private String name;

    @Min(1)
    private int timeout;

    // getters and setters
}

Better IDE Support:

Many IDEs provide better support and autocompletion for @ConfigurationProperties because the configuration properties are well-defined in a separate class. This improves developer productivity and reduces the likelihood of errors.

Scalability:

As the number of configuration properties grows, using @ConfigurationProperties becomes more manageable than scattering multiple @Value annotations throughout your codebase.

@Component
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private int timeout;
    private DatabaseProperties database;

    // getters and setters

    public static class DatabaseProperties {
        private String url;
        private String username;
        private String password;

        // getters and setters
    }
}

Avoiding Duplication:

If you use the same property twice you have to define validation etc. twice.

This is already problematic when having multiple @PDSMustBeDocumented or @MustBeDocumented annotations. You might run into inconsistencies not knowing which documentation is the latest one.

ToDo

Group all @Valueannotated properties into @ConfigurationProperties classes. Make sure @PdsMustBeDocumented annotation still works with @ConfigurationProperties.