2017-09-24 56 views
0

我試圖在自定義ConstraintValidator中工作@Autowired註釋。在validation.xml中添加SpringWebConstraintValidatorFactory時出現StackOverflowError

我讀過一些解決方案,但沒有工作,所以我試圖強制使用Spring的org.springframework.web.bind.support.SpringWebConstraintValidatorFactory 來創建自動裝配的ConstraintValidator實例。

這是什麼文件說:

公共類SpringWebConstraintValidatorFactory:

JSR-303 ConstraintValidatorFactory實現委託給當前春季的WebApplicationContext創建自動裝配Autowired ConstraintValidator實例。 與SpringConstraintValidatorFactory相反,這個變體是用於標準validation.xml文件中的聲明性使用,例如,結合JAX-RS或JAX-WS。

所以我提供了META-INF/validation.xml中

<validation-config 
     xmlns="http://jboss.org/xml/ns/javax/validation/configuration" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration" version="1.1"> 

    <constraint-validator-factory> 
     org.springframework.web.bind.support.SpringWebConstraintValidatorFactory 
    </constraint-validator-factory> 

</validation-config> 

但是,當數據被驗證我得到java.lang.StackOverflowError的

這裏是堆棧跟蹤:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError 
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:982) 
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) 
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:661) 
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317) 
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127) 
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:150) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105) 
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331) 
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) 
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177) 
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) 
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) 
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 

沒有這個配置的應用程序工作也很奇怪。

從一開始就

所以,我有一個使用自動裝配Autowired userService定製約束驗證:

public class EmailValidator implements ConstraintValidator<ValidEmail, String> { 

    @Autowired 
    UserService userService; 

    public void initialize(ValidEmail constraint) { 
    } 

    public boolean isValid(String email, ConstraintValidatorContext context) { 
      return userService.isEmailAvailable(email); 
    } 
} 

,什麼是不可思議只在完全充滿登記表,當我試圖執行測試(甚至不接觸驗證) userService不能被自動裝配,因此它是空的。當我填寫例如只有用戶名和電子郵件,它的okey - 它得到驗證,並且我得到消息在定製costraint中定義,這意味着userService被正確調用,但是當我嘗試驗證具有有效數據的完整註冊表單時 - userService爲null。這就像有時在工作,有時候不是。至少在我讀過的時候,它在這種情況下是如何被驗證的,而不是自動裝配的。

當我嘗試從isValid函數返回語句時,它可以像預期的那樣工作,即使在測試中,但它不是werid?我想爲此提供正常解決方案。這就是爲什麼我試着改變ConstraintValidatorFactory。但也許有不同的方法來解決它?

我想我已經嘗試了Stack的所有解決方案,但都沒有工作。

這是我的測試類是如何配置的,但我不認爲是這樣的話:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = "classpath:/appTestconfig-root.xml") 
@WebAppConfiguration 
public class UserDAOTest { 
    ... 
} 

什麼我失蹤?

這裏是堆棧跟蹤,當我不改變的驗證配置:

javax.validation.ValidationException: HV000028: Unexpected exception during isValid call. 

    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:449) 
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:127) 
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:87) 
    at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:73) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:616) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:581) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:527) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:495) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:460) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:410) 
    at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:207) 
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:116) 
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:80) 
    at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:205) 
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:82) 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:589) 
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463) 
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) 
    at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:50) 
    at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1396) 
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1481) 
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1445) 
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) 
    at org.hibernate.query.internal.AbstractProducedQuery.uniqueResult(AbstractProducedQuery.java:1457) 
    at todolist.daos.UserDAOimpl.getUserByName(UserDAOimpl.java:25) 
    at todolist.daos.UserDAOTest.addUser(UserDAOTest.java:42) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
Caused by: java.lang.NullPointerException 
    at todolist.validators.EmailValidator.isValid(EmailValidator.java:19) 
    at todolist.validators.EmailValidator.isValid(EmailValidator.java:10) 
    at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateSingleConstraint(ConstraintTree.java:446) 
    ... 54 more 

回答

0

哇,我解決了這個問題。

其實問題是完全不同的。

問題在於Hibernate。我不知道它會在每次插入/更新時自動驗證數據。在這種情況下,@Autowired不起作用,因爲Hibernate使用驗證工廠的不同實現。

我在驗證數據之前,這就是爲什麼它不工作時有效。當一切都有效的保存操作被稱爲和休眠重播驗證...

所以我在配置文件中的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 
     ... 

     <property name="javax.persistence.validation.mode">none</property> 

     ... 
    </session-factory> 
</hibernate-configuration> 

禁用它,現在一切工作像預期。

相關問題