Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I apply AOP advice to a ConstraintValidator<A,T> in Spring?
    primarykey
    data
    text
    <p>I am attempting to write my own <code>Validator</code> (as per the JSR-303 specification) under Spring 3.0.x.</p> <p>I annotated the <code>isValid(Serializable, CosntraintValidatorContext)</code> method with <code>@Transactional</code>, but it seems that transactional advice is not being applied to this method - when the <code>isValid()</code> method is invoked, a call to <code>sessionFactory.getCurrentSession()</code> creates the Exception:</p> <blockquote> <p>org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here </p> </blockquote> <p>I have an <code>@Autowired</code> <code>sessionFactory</code> in the same <code>Validator</code> bean which is injected without issues. My current workaround is to use <code>openSession()</code> instead of <code>getCurrentSession()</code>, but that involves manual transaction management which I would prefer to avoid.</p> <p>Doing a stack trace in the constructor of my validator bean, it seems that my <code>Validator</code> bean is being constructed via a call to the <code>o.s.b.f.s.AbstractAutowireCapableBeanFactory.createBean()</code>. I assume this means that the factory is capable of populating <code>@Autowired</code> fields in my <code>Validator</code>, but it seems that it is not applying any transactional advice?</p> <p>Thanks in advance for any help on this matter.</p> <p>EDIT: If it is of any help, I have included a partial stacktrace (via <code>create new Exception()</code>) that is logged in the constructor:</p> <pre><code>at com.mydomain.validators.UniqueValidator.&lt;init&gt;(UniqueValidator.java:49) ~[UniqueValidator.class:na] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [na:1.6.0_20] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) [na:1.6.0_20] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) [na:1.6.0_20] at java.lang.reflect.Constructor.newInstance(Constructor.java:513) [na:1.6.0_20] at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:286) [spring-beans-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory.getInstance(SpringConstraintValidatorFactory.java:50) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.hibernate.validator.engine.ConstraintTree.createAndInitializeValidator(ConstraintTree.java:192) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:171) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:113) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:334) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:278) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:260) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:213) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119) [hibernate-validator-4.0.2.GA.jar:4.0.2.GA] at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:86) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.validation.DataBinder.validate(DataBinder.java:692) [spring-context-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:807) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE] at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414) [spring-webmvc-3.0.5.RELEASE.jar:3.0.5.RELEASE] </code></pre> <p>Transactional configuration appears to be correct (the STS IDE reports that the <code>isValid()</code> method is properly advised by the transactional manager by <code>org.springframework.transaction.interceptor.TransactionInterceptor.invoke</code> (with an <code>@Component</code> annotation on the Validator itself)), but my understanding is that if the validation bean is created outside the normal lifecycle of the Spring container, then the advice will not be properly applied when validation is done during data binding.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload