从外部文件夹加载 Spring 资源类
我有一个 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
conf
目录设置为 Maven 资源目录(maven 构建帮助程序插件),或将文件从conf
移动到src\main\resources
。然后修改ResourceBundleMessageSource
以从类路径根加载消息,因为src\main\resources
对应于类路径根。注意:如果您使用
ReloadableResourceBundleMessageSource
而不是ResourceBundleMessageSource
,那么您必须使用classpath:
前缀作为basename
代码> 属性值 (classpath:messages
)Make the
conf
directory a maven resource directory (maven build helper plugin) or move the files fromconf
tosrc\main\resources
. Then modify theResourceBundleMessageSource
to load the messages form classpath root, becausesrc\main\resources
correspond to the classpath root.Attention: If you use
ReloadableResourceBundleMessageSource
instead ofResourceBundleMessageSource
then you have to use the prefixclasspath:
for thebasename
propertz value (classpath:messages
)