Sumit Ghosh
Sumit Ghosh

Reputation: 514

MessageSource getMessage method not working in Spring mvc

I have developed a Spring mvc application where I want to read some property value from properties file located inside of weblogic server.

Have performed following steps:

Created a project specific folder appConfig in following path: Oracle/Middleware/ORACLE_HOME/user_projects/domains/wl_server/config/rmsConfig

Placed the properties file named commonConfig.properties inside of it.

Have also edited setDomainEnv.cmd with following entry,

if NOT "%EXT_POST_CLASSPATH%"=="" (
   set EXT_POST_CLASSPATH=%EXT_POST_CLASSPATH%;%DOMAIN_HOME%\config\appConfig
if NOT "%POST_CLASSPATH%"=="" (
    set POST_CLASSPATH=%POST_CLASSPATH%;%EXT_POST_CLASSPATH%
) else (
    set POST_CLASSPATH=%EXT_POST_CLASSPATH%
 )
)

Please find below my Spring bean configuration file for this:

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

 <bean id="commonProps" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
      <property name="basename" value="commonConfig" />
      <property name="cacheSeconds" value="120" />
 </bean>
</beans>

In my Java Spring bean class I am refering to this as follows:

         @Component 
         public class LocationClient{
            @Autowired
            private MessageSource commonProps;

           public void showMessage(){
             System.out.println(commonProps.getMessage("common.line", null, null, null)); 
          }
       }

Now commonProps is not null but "common.line" printing null in console.

Please find below property file entry:

common.line=382

Anyone has any suitable solution to this???

Upvotes: 0

Views: 2690

Answers (2)

Daniel C.
Daniel C.

Reputation: 5758

One possible solution is to use ResourceBundleMessageSource instead of ReloadableResourceBundleMessageSource in order to define your properties like this:

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

Also make sure that commonConfig.properties is inside of your WEB-INF folder, for example if your application is appConfig then it should be on the root of your classpath: appConfig/WEB-INF/classes/commonConfig.properties

Second alternative if you need commonConfig.properties outside classpath, using your first approach (make sure that the bean id is messageSource)

 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
      <property name="basename" value="file:/your/absoulute/path/commonConfig" />
      <property name="cacheSeconds" value="120" />
 </bean>

Upvotes: 0

Deepak Singh
Deepak Singh

Reputation: 411

I think you have to add your property file in classpath or in WEB-INF folder.

This is the note from the Java Spring docs.

For a typical web application, message files could be placed in WEB-INF: e.g. a "WEB-INF/messages" basename would find a "WEB-INF/messages.properties", "WEB-INF/messages_en.properties" etc arrangement as well as "WEB-INF/messages.xml", "WEB-INF/messages_en.xml" etc. Note that message definitions in a previous resource bundle will override ones in a later bundle, due to sequential lookup.

Refer the link for more information: ReloadableResourceBundleMessageSource

Upvotes: 0

Related Questions