0

spring.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">  

  <bean id="meassageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="resource\message">  
    </property>
  </bean>

 </beans>

Main.java class file

public class Main {

    public static void main(String[] args) {

        ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml");

        System.out.println(context.getMessage("emp", null, Locale.US));

    }

} 

My properties file is in src/resource folder. File name is mesaage_en_US.properties. I have also tried with different file names like message.property, message_en.property and with different locales like Locale.English, Locale.UK but no luck. I moved the property file to src folder but getting same exception. I am getting following exception.

Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'emp' for locale 'en_US'.
    at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65)
    at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1234)
    at org.beans.Main.main(Main.java:14)

Please help.

message_en_US.properties

emp=Hello Employee.
Jitendra Shukla
  • 26
  • 1
  • 1
  • 7
  • Sounds like your `ApplicationContext` is not auto-detecting your `MessageSource`. The bean name has a typo in it - is that how it is in your actual code? – superEb Jul 28 '13 at 21:02
  • Classpath properties should be separated by forward slashes so try resource/message. – samlewis Jul 28 '13 at 21:05
  • @superEb There is no typo in actual code. – Jitendra Shukla Jul 28 '13 at 21:42
  • Have you tried what @samlewis suggested? Also, what if you get direct access to the `messageSource` bean by calling `ApplicationContext.getBean` and then calling `getMessage` against it? – superEb Jul 28 '13 at 22:32

5 Answers5

6

I like to use a PropertySourcesPlaceholderConfigurer for that. Here's a great tutorial to get you started.

Basically, you'll want to add:

<context:property-placeholder location="classpath:foo.properties" />

to your spring xml config file, where "foo.properties" is a resource's absolute path within the class path.

Then you can inject them into fields like this:

@Value( "${jdbc.url}" )
private String jdbcUrl;

where "jdbc.url" is the reference name in your properties file.

Of course, the @Value won't work inside your static void main, but I really doubt static void main is where you want to use your properties anyway. You ought to be accessing them from a Spring Bean.

CorayThan
  • 17,174
  • 28
  • 113
  • 161
  • Right but its Test App. – Jitendra Shukla Jul 28 '13 at 20:52
  • Well, I would assert that when making a Spring test app, you should make it the way it should be made with Spring. So you should be testing that you can access the properties from Spring components, not directly from the application context. [This question](http://stackoverflow.com/questions/10822951/how-do-i-get-a-property-value-from-an-applicationcontext-object-not-using-an-a) has an answer that explains why you can't access property-placeholders from the context at runtime. – CorayThan Jul 28 '13 at 20:59
  • I have also tried by implementing MessageSourceAware interface in different bean. This is also not working. – Jitendra Shukla Jul 28 '13 at 21:15
  • While this is a good suggestion for configuration values, it does not address i18n, which is the main purpose of `MessageSource`. – superEb Jul 30 '13 at 22:03
0

Change to

<property name="basename" value="message" />

with message_en_US.properties in the same folder as your spring.xml.

EDIT : You have a typo in your bean name when defining the MessageSource. It's name should have been exactly messageSource. Because of that extra a in meassageSource ApplicationContext failed to load it.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

I think this is duplicated from this question. Basically, it has to do with a mismatch between your bundle and the locale specified in code.

Community
  • 1
  • 1
Juan Carlos González
  • 1,024
  • 2
  • 9
  • 22
0

Instead of getting message from ApllicationContext I am getting message from MeassageSource itself. I changed my spring.xml like this

<bean id="employee" class="org.bean.Employee" >
         <property name="id" value="1"/>
         <property name="name" value=""/>
         <property name="dept" value=""/>  
         <property name="messages" ref="messageSource"/>    
      </bean>

Now I am calling messages.getMessage(this.messages.getMessage("emp", null, Locale.UK)) from Employee class. Its working.

Jitendra Shukla
  • 26
  • 1
  • 1
  • 7
0

I've reproduced your error and found the problem. It has to do with Spring not finding the bundle. I think you should be getting a warning before the exception with the following message:

WARNING: ResourceBundle [resource\message] not found for MessageSource: Can't find bundle for base name resource\message, locale en_US

This has been the hint. The problem is related to your project structure and how the bundles are searched when specifying setBasename property. Please take a look at this.

Anyway I think you should put your bundles in the more standard location src/main/resources. If you follow this convention, your messageSource bean should be defined like this:

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

With this approach your example should produce the desired line:

Hello Employee.

Juan Carlos González
  • 1,024
  • 2
  • 9
  • 22