Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Convention for Variables

So I have an application.yml file for my spring boot application like so:

spring:
  url: localhost
email:
  from: [email protected]
app:
  uuid: 3848348j34jk2dne9

I want to wire these configuration properties into different components in my app like so:

@Component
public class FooA {
    private final String url;

    public FooA(@Value("${spring.url}") String url) {
        this.url = url
    }
}

@Component
public class FooB {
    private final String from;

    public FooA(@Value("${email.from}") String from) {
        this.from = from
    }
}

@Component
public class FooC {
    private final String uuid;

    public FooA(@Value("${app.uuid}") String uuid) {
        this.uuid = uuid
    }
}

The above works as intended in my application. But my question is if this is best practice in spring boot. The only other alternative to this that I know of is to use a Properties object by creating a bean inside of a configuration class, loading the properties with all the configuration variables and autowire the property bean into the components.

What is the best practice in this case?

like image 695
Richard Avatar asked Aug 30 '17 23:08

Richard


People also ask

What is @configuration in Spring boot?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

Does Spring boot Favours over convention?

Spring Boot enables developers to focus on the application logic rather than being bogged down by the intricacies of configuration. Spring has always prioritised convention over configuration as a model for simpler programming and Spring Boot Project emphasizes a similar discipline.

What is @RefreshScope in Spring boot?

Now, you need to add the @RefreshScope annotation to your main Spring Boot application. The @RefreshScope annotation is used to load the configuration properties value from the Config server.


1 Answers

As you have identified your two main choices for injecting configuration are using either @Value on individual properties or @ConfigurationProperties on a javabean configuration object.

Which one you use comes down to preference. Personally I prefer using a configuration object.

Using @ConfigurationProperties allows you to use JSR-303 bean validation.
You can also write your own custom validation in the setter of your javabean if you desire.
You can annotate config beans from non-spring projects, which allows you to write libraries that are easily configured but don’t depend on spring.
You can generate IDE meta-data from the objects that may make your development process smoother.

Here are some practices I recommend when using spring configuration.

  • Create individual @ConfigurationProperties objects for logical components of your application. Try to keep things modular and avoid creating a dumping ground for your entire applications config.

  • Don’t use the same @Value property in multiple locations.
    If the same configuration is needed in multiple places within your application then you should probably move the value into a config object.
    Using the same property across multiple @Value annotations makes it harder to reason around, and it can also cause unexpected behaviour if you define a default value by way of a "SpEL" expression in one place and not the other.

  • Don't define your own properties in the spring namespace.
    For example your spring.url property is not one of the properties defined in the documentation.
    By using the same name space you run the risk of that name being used for something in future spring-boot versions.

like image 132
Magnus Avatar answered Sep 28 '22 03:09

Magnus