Announcement Announcement Module
Collapse
No announcement yet.
Annotation autowiring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation autowiring

    Hi all,
    I'm using spring 2.5 and tomcat 6.0. I'm having a problem with regard to annotation based auto wiring.
    I have a user sign up page where it is required to check whether username is already taken in validation in form submit.
    For this I have used service class called UserManager. Eventhough I have autowired UserManager in UserValidator class when form is submitted, a null pointer exception thrown saying userManager property of UserValidator class is null. Below I have listed relevant code.. Any help would be greatly appreciated.

    Code:
    <at>Controller
    <at>RequestMapping("sign_up")
    public class RegisterController {
    
    	<at>Autowired
    	private UserManager userManager;
    
    	<at>RequestMapping(method = RequestMethod.GET)
    	public String setupForm(ModelMap model) {
    		logger.info("setup sign up");
    		model.addAttribute("user", new User());
    		return "sign_up";
    	}
    
    	<at>RequestMapping(method = RequestMethod.POST)
    	public String processSubmit(<at>ModelAttribute("user")
    	User user, BindingResult result, SessionStatus status) {
    		Validator validator = new UserValidator();
    		validator.validate(advertiser, result);
    		if (result.hasErrors()) {
    			logger.info("validation failed: " + result.getFieldErrors());
    			return "sign_up";
    		} else {
    			userManager.addUser(user);
    			return "redirect:account_created";
    		}
    		
    	}
    
    }
    
    
    
    public class UserValidator implements Validator {
    
    	<at>Autowired
    	private UserManager userManager;
    	
    	public boolean supports(Class clazz) {
    		return User.class.equals(clazz);
    	}
    
    	public void validate(Object obj, Errors errors) {
                    User user = (User) obj;
    		....
    		if(userManager.userExists(user.getUsername())){
    			errors.rejectValue("username","error.userexists", null,"Username already taken.");
    		}
    
                    ......
    	}
    
    }
    
    
    
    
    <at>Service("userManager")
    public class UserManagerImpl implements UserManager {
    	protected Log logger = LogFactory.getLog(getClass());
    	
    	<at>Autowired
    	private UserDao userDao;
    
            .............
    	public boolean userExists(String name) {
    		return userDao.isUser(name);
    	}
            ........
    
    
    }
    
    
    
    <at>Repository("userDao")
    public class JdbcUserDao extends SimpleJdbcDaoSupport implements
    		UserDao {
    
    	<at>Autowired
    	public JdbcUserDao(DataSource dataSource){
    		setDataSource(dataSource);
    	}
    
    
    	public boolean isUser(String username) {
    		List<User> users = getSimpleJdbcTemplate().query(
    				"SELECT * FROM user WHERE username=:username",
    				new UserMapper(),
    				new MapSqlParameterSource().addValue("username", username));
    		if(users.isEmpty()){
    			return false;
    		}else{
    			return true;
    		}
    	}
             .....
    
    
    }

  • #2
    Well if you do

    Code:
    Validator validator = new UserValidator();
    How do you expect Spring to inject dependencies? You are creating a new one yourself outside the control of Spring. Inject the validator into your controller like your userManager.

    Comment


    • #3
      Thanks Marten!
      Actually didn't think about that

      Comment

      Working...
      X