How to make simple property validation when using Spring @Value

Tomasz

How can I check, if ${service.property} is not an empty string and if so, throw some kind of readable exception? It must happen during Bean creation.

@Component
public class Service {

  @Value("${service.property}")
  private String property;
}

I am looking for the easiest way (least written code). Would be great if by using annotations.

My current solution is to perform "handwritten" validation inside setter for the property, but is a little too much code for such easy thing.

Hint: I looked for some way to use the SpEL, since I use it already inside @Value, but as far I have found out, it would not be that easy/clean. But could have overlooked something.

Clarification: Expected behaviour is, that the application will not start up. The goal is to assure, that all properties are set, and especially, that string properties are not empty. Error should say clearily, what is missing. I don't want to set any defaults! User must set it all.

Felipe Desiderati

You can use the component as a property placeholder itself. And then you may use any validation that you want.

@Component
@Validated
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
public class MyService {

    @NotBlank
    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    ...
}

And your my.properties file will look like this:

my.username=felipe

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to make camel simple expression work with property placeholder in spring xml

From Dev

how to configure messages property for validation using annotations spring

From Dev

How to decrypt a property value when using PropertySourcesPlaceholderConfigurer

From Dev

how to use Spring-EL in @Value when using constants to resolve a property

From Dev

How to make Coding Simple Request Form Validation

From Dev

How to load Name Value property using @ConfigurationProperties in Spring

From Dev

Spring MVC form validation using Value Object

From Dev

Why is the property value null when using Spring AOP but the same value works via getter?

From Dev

How to make more than 2 value show in 2 textboxes using the value property

From Dev

How to make that variable become a value of css property?

From Dev

How to extract value from JSON response when using Spring MockMVC

From Dev

How often does as spring property get updated when value in application.properties changes?

From Dev

How to make a simple HTML playlist using Jquery

From Dev

How to make simple animation using ngAnimate

From Dev

How do I use CodeIgniter's `set_value()` function when using `form_validation.php`?

From Dev

Is there more overhead when using a simple property over a member?

From Java

In React how to get a property value of an object using another property value?

From Dev

how to retrieve the value of a property using the value of another property in RDDs

From Dev

how to retrieve the value of a property using the value of another property in RDDs

From Dev

Spring: How to Resolve a Property When There Are Multiple Resolvers?

From Dev

Spring: How to Resolve a Property When There Are Multiple Resolvers?

From Dev

validation with double value in Spring

From Dev

How to find port of Spring Boot container when running a spock test using property server.port=0

From Dev

How to check if a value is valid when set property

From Dev

How to query an entity property by null using spring?

From Dev

How to query an entity property by null using spring?

From Dev

property-based-test for simple object validation

From Dev

How to do simple cross field validation in Angular 2 form to pass validation if one of the control in group has value?

From Dev

How to make text box border red when validation fail

Related Related

  1. 1

    how to make camel simple expression work with property placeholder in spring xml

  2. 2

    how to configure messages property for validation using annotations spring

  3. 3

    How to decrypt a property value when using PropertySourcesPlaceholderConfigurer

  4. 4

    how to use Spring-EL in @Value when using constants to resolve a property

  5. 5

    How to make Coding Simple Request Form Validation

  6. 6

    How to load Name Value property using @ConfigurationProperties in Spring

  7. 7

    Spring MVC form validation using Value Object

  8. 8

    Why is the property value null when using Spring AOP but the same value works via getter?

  9. 9

    How to make more than 2 value show in 2 textboxes using the value property

  10. 10

    How to make that variable become a value of css property?

  11. 11

    How to extract value from JSON response when using Spring MockMVC

  12. 12

    How often does as spring property get updated when value in application.properties changes?

  13. 13

    How to make a simple HTML playlist using Jquery

  14. 14

    How to make simple animation using ngAnimate

  15. 15

    How do I use CodeIgniter's `set_value()` function when using `form_validation.php`?

  16. 16

    Is there more overhead when using a simple property over a member?

  17. 17

    In React how to get a property value of an object using another property value?

  18. 18

    how to retrieve the value of a property using the value of another property in RDDs

  19. 19

    how to retrieve the value of a property using the value of another property in RDDs

  20. 20

    Spring: How to Resolve a Property When There Are Multiple Resolvers?

  21. 21

    Spring: How to Resolve a Property When There Are Multiple Resolvers?

  22. 22

    validation with double value in Spring

  23. 23

    How to find port of Spring Boot container when running a spock test using property server.port=0

  24. 24

    How to check if a value is valid when set property

  25. 25

    How to query an entity property by null using spring?

  26. 26

    How to query an entity property by null using spring?

  27. 27

    property-based-test for simple object validation

  28. 28

    How to do simple cross field validation in Angular 2 form to pass validation if one of the control in group has value?

  29. 29

    How to make text box border red when validation fail

HotTag

Archive