question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use configuration classes instead of @Value annotations

See original GitHub issue

You’re using @Value about a dozen times, which isn’t considered best practice anymore.

To quote Thomas Uhrig (@tuhrig):

What will happen when you use this technique through your application? Well, whenever you need some property, you will inject it. You will inject it in services, controllers and components and you will properly inject the same property in different classes. That’s easy and absolutely what the mechanism is about.

However, this means nothing else than scattering your configuration about your whole application. Every class can pick a couple of properties to use. You will have no idea or control which class is using which properties. You will end-up with doing a full text search on your project to find out where a single key is used. You will have a lot of fun if you want to rename one of those keys or when you need to set each and every property for each and every class in your unit tests. […] If you do so [use a configuration class], you will have a single point of responsibility to load and get your configuration from. As any other service, you can easily change the implementation. Maybe you don’t want to load your properties from a properties file, but from a database or web service! (source)

While this issue wouldn’t qualify as high priority right now, it’ll probably help in the future.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jwedelcommented, May 19, 2020

@MALPI You can have a look at the spring boot docs regarding the @ConfigurationProperties annotation:

The way to go would be to keep the property keys the same and use a separate class that binds the common key prefix (e.g. services.submission) and provides type safe config values. Then inject that into the class that currently uses @Value.

See here for an example:

2reactions
MALPIcommented, May 19, 2020

Hey there,

I’d like to contribute and work on this issue?

BTW: Great that you uses the good first issue label.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When should you use @Configuration instead of @Service ...
short answer. @Component in a java class is to tell to spring: Register me automatically at the startup in your context so any...
Read more >
Spring @Configuration Annotation - DigitalOcean
Spring @Configuration annotation allows us to use annotations for dependency injection. Let's understand how to create Spring Configuration ...
Read more >
4.3. Aggregating @Configuration classes with @Import - Spring
Often it is preferable to use an aggregation approach, where one @Configuration class logically imports the bean definitions defined by another.
Read more >
Java Spring Boot - Tutorial @Configuration @Value - YouTube
Hi! In this video tutorial I am showing example how to access application properties by using @ Configuration class annotation and @ Value...
Read more >
Spring @Configuration Annotation with Example
One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found