Search code examples
javaspring-bootsyntaxannotations

What is the method signature means? String value() default "";


I came across this method signature in Spring Component interface.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component 
{
   String value() default "";
}

What is the method signature String value() default ""; means? How and When should we define such syntax for our coding purposes?


Solution

  • This is no method signature. it means that you can pass a String as parameter to the Component annotation, like this:

    @Component(value = "value")
    

    If you don't specify a value your self, the default value "" will be used.

    If it had been like this:

    String value(); // without the default
    

    value would have been a mandatory parameter. Trying to use Component like this:

    @Component()
    

    would lead to an Exception, since you didn't provide a value.

    EDIT: when to use.

    If you don't know much about this syntax, or annotations in general, you shouldn't use them. About everything that can be done using annotations, especially custom made ones, can also be done without annotations.

    Let's say you want to create an annotation to validate the value of a field. I'll be using the example of Belgian postal codes. They all are 4 digits, and are between 1000 and 9999.

    @Target( {ElementType.FIELD})
    @Retention( RetentionPolicy.RUNTIME)
    @Constraint( validatedBy = ValidatePostalCodeImpl.class)
    public @interface ValidatePostalCode{
      String message() default "You have entered an invalid postal code";
      Class<?>[] groups() default {}; // needed for the validation
      Class<? extends Payload>[] payload() default{}; // needed for the validation
    
      int maxValue() default 9999; // so, by default, this will validate based
      int minValue() default 1000; // on these values, but you will be able to 
      // override these
    }
    

    /* Validation implementation */

    public class ValidatePostalCodeImpl implements ConstraintValidator<ValidatePostalCode, Integer> {
    
        int upperValue;
        int lowerValue;
    
        @Override
        public void initialize(ValidatePostalCode validate) {
            this.upperValue = validate.maxValue(); // here you call them as if they indeed were regular methods
            this.lowerValue = validate.minValue();
        }
    
        @Override
        public boolean isValid(Integer integer, ConstraintValidatorContext context) {
            return integer >= lowerValue && integer <= upperValue;
        }
    
    }
    

    /* Usage */

    @Entity
    @Table(name = "addresses")
    public class Addresses {
    
      // home address -> In Belgium, so has to be between the default values:
      @ValidatePostalCode
      Integer belgianPostalCode;
    
      // vacation home in another country, let's say the PC's there are between
      // 12000 and 50000
      @ValidatePostalCode(minValue = 12000, maxValue = 50000)
      Integer foreignPostalCode;
    
    }
    

    Sure, this is a very limited example, but it should get you an idea.