How to @Autowire objects in Validator classes?

前端 未结 2 1719
有刺的猬
有刺的猬 2021-02-14 06:36

Is it possible to Autowire an object in a Validation class? I keep getting null for the object that is supposed to be Autowired...

2条回答
  •  借酒劲吻你
    2021-02-14 06:46

    Are your Validation class an enabled Spring bean ??? If not, you always will get null for your object autowired. Make sure you have enabled your Validation class.

    And do not forget enable The Annotation config bean post-processor (see element)

    
        
    
    

    How to enable your Validation class as a managed Spring bean. Either

    By using xml (As shown above)

    
        
        
    
    

    By using annotation instead (Notice @Component just above class)

    @Component
    public class AccessRequestValidator implements Validator {
    
    }
    

    But to enable Spring annotated component scanning, you must enable a bean-post processor (notice

    
        
        
    
    

    Inside your Controller, just do it (Do not use new operator)

    Choose one of the following strategies

    public class MyController implements Controller {
    
        /**
          * You can use FIELD @Autowired
          */
        @Autowired
        private AccessRequestValidator accessRequestValidator;
    
        /**
          * You can use PROPERTY @Autowired
          */
        private AccessRequestValidator accessRequestValidator;
        private @Autowired void setAccessRequestValidator(AccessRequestValidator accessRequestValidator) {
            this.accessRequestValidator = accessRequestValidator;
        }
    
        /**
          * You can use CONSTRUCTOR @Autowired
          */
        private AccessRequestValidator accessRequestValidator;
    
        @Autowired
        public MyController(AccessRequestValidator accessRequestValidator) {
            this.accessRequestValidator = accessRequestValidator;
        }   
    
    }
    

    UPDATE

    Your web app structure should looks like

    /
           WEB-INF/
               web.xml
               -servlet.xml
               business-context.xml
               classes/
                   /com
                       /wuntee
                           /taac
                               /validator
                                   AccessRequestValidator.class
               lib/
                   /**
                     * libraries needed by your project goes here
                     */
    

    Your web.xml should looks like (NOTICE contextConfigLocation context-param and ContextLoaderListener)

    
        
            contextConfigLocation
            
            
            
                /WEB-INF/business-context.xml
            
        
        
            org.springframework.web.context.ContextLoaderListener
        
        
            
            org.springframework.web.servlet.DispatcherServlet
            1
        
        
            
            *.htm
        
    
    

    Your -servlet.xml should looks like (Notice i am using Spring 2.5 - replace if you are using 3.0)

     
        
        
        
        
    
    

提交回复
热议问题