Adi Mor
Adi Mor

Reputation: 2165

Spring resource class loading from an external folder

I have a spring project name: SpringExample‬

the Directory Structure:

enter image description here

the locale.xml for the spring :

<bean id="customerService" class="com.mkyong.customer.services.CustomerService" />

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename">
        <value>\SpringExample\conf‬\messages</value>
    </property>
</bean>

the code for CustomerService:

 package com.mkyong.customer.services;


    public class CustomerService implements MessageSourceAware
    {
        private MessageSource messageSource;

        public void setMessageSource(MessageSource messageSource) {
            this.messageSource = messageSource;
        }

        public void printMessage(){
            String name = messageSource.getMessage("customer.name", 
                    new Object[] { 28, "http://www.mkyong.com" }, Locale.US);
            System.out.println("Customer name (English) : " + name);

    }

the code for the app class (the main class):

package com.mkyong.common;

import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "locale.xml" });

        // get the message resource inside context
        String name = context.getMessage("customer.name", new Object[] { 28,
                "http://www.mkyong.com" }, Locale.US);
        System.out.println("Customer name (English) : " + name);


        // get the message resource inside the bean
        CustomerService cust = (CustomerService) context
                .getBean("customerService");
        cust.printMessage();
    }
}

But it fails to bundle the proprtie file, because it isn't in the package. i get the exception:

WARNING: ResourceBundle [\SpringExample\conf?\messages] not found for MessageSource: Can't find bundle for base name \SpringExample\conf?\messages, locale en_US

how can i get it to bundle with external folder resource like in my example?

Upvotes: 1

Views: 3687

Answers (1)

Ralph
Ralph

Reputation: 120861

Make the conf directory a maven resource directory (maven build helper plugin) or move the files from conf to src\main\resources. Then modify the ResourceBundleMessageSource to load the messages form classpath root, because src\main\resources correspond to the classpath root.

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

Attention: If you use ReloadableResourceBundleMessageSource instead of ResourceBundleMessageSource then you have to use the prefix classpath: for the basename propertz value (classpath:messages)

Upvotes: 3

Related Questions