4

I have made custom annotation for validation of my bean field. I use @Age(value = 10) annotation for validation age. I have write code as below.

import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
import javax.validation.Constraint;  
import javax.validation.Payload;  



@Documented
@Constraint(validatedBy = AgeConstraintValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Age {

    String message() default "{Age is not valid }";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    int value();
}

This is code for age constrain validator

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class AgeConstraintValidator   implements ConstraintValidator< Age, Long> {

    private Age age;

    @Override
    public void initialize(Age age) {
        this.age = age;
    }

    @Override
    public boolean isValid(Long dob, ConstraintValidatorContext context) {
        System.out.print(" age in annotion");
              if(dob != age.value()){
                     return true;
                 }
        return false;
    }

}

Now when i use @Age( value = 10) in my bean so it is not called Age annotation. Can anyone tell me any fault in my code. When i create my bean object and assign age differ for test but i can not get any validation on bean 's field .

Thanga
  • 7,811
  • 3
  • 19
  • 38
Anuj Dhiman
  • 1,550
  • 3
  • 26
  • 51
  • You need to point out this Annotation in your configuration. What frameworks are you using? Spring? Try with annotating the Anntation with @Component if you have spring auto-lookup. Also show where and how you are using this annotation. Maybe you'll need to use CGLIB – Krzysztof Cichocki Feb 25 '16 at 11:01
  • @KrzysztofCichocki i am using spring – Anuj Dhiman Feb 25 '16 at 11:03
  • Show the class where you are using this annotation – Krzysztof Cichocki Feb 25 '16 at 11:05
  • Class User { @Age( value =10) int age ; //getter and setter } – Anuj Dhiman Feb 25 '16 at 11:08
  • Ok, so you need to use cglib in order to proxy the access to a field. You shold use the AOP framework. Look at this post for hints: http://stackoverflow.com/questions/1706751/retain-annotations-on-cglib-proxies – Krzysztof Cichocki Feb 25 '16 at 11:14
  • Can you please provide the solution? I am struggling with the same issue and I am not able to figure out the solution. It will be really helpful. – Tushar Banne Jan 09 '18 at 09:49

1 Answers1

1

Spring will not take this custom annotation automatically. You have to let Spring know about by defining a BeanPostProcessor. create a class which implements it For e-g

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;

public class InitHelloWorld implements BeanPostProcessor {

   public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      System.out.println("BeforeInitialization : " + beanName);
      return bean;  // you can return any other object as well
   }

   public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      System.out.println("AfterInitialization : " + beanName);
      return bean;  // you can return any other object as well
   }

}

and mention about this bean in your spring config xml as below

 <bean class="<your pack>.InitHelloWorld" />
Thanga
  • 7,811
  • 3
  • 19
  • 38