SoftwareDeveloper
SoftwareDeveloper

Reputation: 1162

Spring MessageSource not getting message from properties file

I did some googling around and found my problem have appeared many times but i have tried all suggested solutions but it is still not working for me

I am keep getting this exception:

org.springframework.context.NoSuchMessageException: No message found under code 'error.null.firstname' for locale 'en'.

here is my setup in my dispatcher-servlet.xml file i have:

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value = "error"/>
</bean>

i have a properties file (error_en.properties) under src/main/resources/ directly and it does contain the following line:

error.null.firstname=Firstname cannot be null

in my code i am trying:

    @Autowired
    private MessageSource messageSource;
    ...
    System.out.println(messageSource.getMessage("error.null.firstname", null, Locale.ENGLISH));

but above is not working and is giving me exception mentioned above

I've tried prefixing "classpath:" to 'value' in value attribute but that didn't work for me. I've ensured that the filename 'error' matches with the value attribute because i know '_en' would be dealt with by Spring Framework.

I can't see what am i missing?

Upvotes: 4

Views: 8279

Answers (1)

SoftwareDeveloper
SoftwareDeveloper

Reputation: 1162

after days of experimenting/debugging - finally figured out what the problem is.

The class where i'm autowiring the MessageSource was actually belong to a different context.

My messageSource bean definition was declared in my dispatcher-Servlet.xml context file because i'm developing a web application.

Whereas my business class was declared in application-context.xml so it couldn't find the MessageSource.

After i've moved everything into the dispatcher-Servlet.xml file it work. Probably not an elegant solution but that solved my problem.

Thanks for all help previously

Upvotes: 1

Related Questions