Загрузка класса ресурсов Spring из внешней папки - PullRequest
1 голос
/ 19 февраля 2012

У меня есть имя проекта Spring: SpringExample

Структура каталогов:

enter image description here

locale.xml для весны:

<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>

код для 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);

    }

код для класса приложения (основной класс):

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();
    }
}

Но он не может связать файл proprtie, потому что его нет в пакете. я получаю исключение:

ВНИМАНИЕ: ResourceBundle [\ SpringExample \ conf? \ Messages] не найден для Источник сообщения: не удается найти пакет для базового имени \ SpringExample \ conf? \ Messages, locale ru_US

как я могу получить его в комплекте с ресурсом внешней папки, как в моем примере?

1 Ответ

3 голосов
/ 19 февраля 2012

Сделайте каталог conf каталогом ресурсов maven (плагин помощника сборки maven) или переместите файлы из conf в src\main\resources.Затем измените ResourceBundleMessageSource для загрузки сообщений из корня пути к классам, поскольку src\main\resources соответствует корню пути к классам.

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

Внимание: если вы используете ReloadableResourceBundleMessageSource вместо ResourceBundleMessageSourceВы должны использовать префикс classpath: для значения свойства basename (classpath:messages)

...