File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Create a Validator using .properties file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Create a Validator using .properties file" Watch "Create a Validator using .properties file" New topic

Create a Validator using .properties file

Anupam Dee
Ranch Hand

Joined: Oct 18, 2010
Posts: 42

I want to create a Validator class e.g PersonValidator that implements Validator interface. I want to get the error messages stored in the .properties file (for Internalization).
I am trying to autowire MessageResource to do the same.

this is the xml content

and this is main

and the output is
[Field error in object 'person' on field 'name': rejected value [null]; codes [,,,name.required]; arguments []; default message [null]]

the first null is messageSource getting printed and it is null !
I think i am not able to autowire the messagesource to the .properties file. How should i do it?
Any help would be appreciated.

Thanks in advance.
Bill Gorder

Joined: Mar 07, 2010
Posts: 1682

MessageSource is null because it is not a Spring managed bean you are creating it using the new operator. Get it from the context instead.

Of course you will first have to make sure that the "personValidator" bean is defined in your context and you are wiring the messageSource into it. You could do this by adding an @Component to the PersonValidator class and adding a component scan or you could just wire it up manually.

[How To Ask Questions][Read before you PM me]
I agree. Here's the link:
subject: Create a Validator using .properties file
It's not a secret anymore!