Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In our project we have Service and DAO layers in separate Maven modules. Service module depends on DAO module and works with it's entities. The problem is that we can't put custom jsr 303 constraint which uses some services from service layer in DAO entity because that would create the back reference from DAO layer to service layer, because the validator class needs to be referenced in validatedBy attribute of custom @Constraint.

Is there a way (using standard jsr 303 api) to specify validator class of some custom constraint at runtime (or solve our problem in any other way)? The built in constraints have empty validatedBy attribute, but I don't know if there is some api for that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
98 views
Welcome To Ask or Share your Answers For Others

1 Answer

We faced the same issue in our Spring based project. To solve it in best Spring way we split ConstraintValidator interface and implementation. For example in domain layer we only have interface:

public interface UniqueValidator extends ConstraintValidator<Unique, String> {
}

In service layer we implement that interface:

public class UniqueValidatorJpaImpl implements UniqueValidator {
    private EntityManager entityManager;
    ...
}

Next we declare a bean in Spring Context for UniqueValidatorJpaImpl.

Finally to make all that staff working we extended SpringConstraintValidatorFactory. By default it only creates a new instance of class specified in validatedBy. We extended it by first looking in spring context for a bean of corresponding type:

public class SpringConstraintValidatorFactoryEx implements ConstraintValidatorFactory {

    private final Logger logger = LoggerFactory.getLogger(SpringConstraintValidatorFactoryEx.class);

    @Autowired
    private AutowireCapableBeanFactory beanFactory;

    public <T extends ConstraintValidator<?, ?>> T getInstance(Class<T> key) {
        T bean = null;

        try {
            logger.info("Trying to find a validator bean of class " + key.getSimpleName());
            bean = this.beanFactory.getBean(key);
        } catch (BeansException exc) {
            logger.info("Failed to find a bean of class " + key.getSimpleName());
        }

        if (bean == null) {
            try {
                logger.info("Creating a new validator bean of class " + key.getSimpleName());
                bean = this.beanFactory.createBean(key);
            } catch (BeansException exc) {
                logger.info("Failed to create a validator of class " + key.getSimpleName());
            }
        }

        if (bean == null) {
            logger.warn("Failed to get validator of class " + key.getSimpleName());
        }

        return bean;
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...