从外部文件夹加载 Spring 资源类

发布于 2025-01-07 00:33:14 字数 2269 浏览 0 评论 0原文

我有一个 spring 项目名称: SpringExample

目录结构:

在此处输入图像描述

spring 的 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] MessageSource:找不到基本名称的包 \SpringExample\conf?\messages, 语言环境 en_US

我怎样才能将其与外部文件夹资源捆绑在一起,就像我的示例一样?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2025-01-14 00:33:14

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)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文