2013-07-28 76 views
0

spring.xml读取属性文件在Spring 3.2

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

    <bean id="meassageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="resource\message"> 
    </property> 
    </bean> 

</beans> 

Main.java类文件

public class Main { 

    public static void main(String[] args) { 

     ApplicationContext context= new ClassPathXmlApplicationContext("spring.xml"); 

     System.out.println(context.getMessage("emp", null, Locale.US)); 

    } 

} 

我的属性文件在src /资源文件夹。文件名是mesaage_en_US.properties。 我也尝试过不同的文件名,如message.property,message_en.property和不同的语言环境,如Locale.English,Locale.UK,但没有运气。 我将属性文件移动到src文件夹,但得到相同的异常。 我收到以下异常。

Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'emp' for locale 'en_US'. 
    at org.springframework.context.support.DelegatingMessageSource.getMessage(DelegatingMessageSource.java:65) 
    at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1234) 
    at org.beans.Main.main(Main.java:14) 

请帮忙。

message_en_US.properties

emp=Hello Employee. 
+0

听起来像你的'ApplicationContext'不能自动检测你的'MessageSource'。该bean的名称中有一个错字 - 它是如何在你的实际代码中? – superEb

+0

类路径属性应该用正斜杠分隔,所以请尝试资源/消息。 – samlewis

+0

@superEb在实际代码中没有错别字。 –

回答

5

我喜欢用一个PropertySourcesPlaceholderConfigurerHere's a great tutorial让你开始。

基本上,你需要添加:

<context:property-placeholder location="classpath:foo.properties" /> 

你的Spring XML配置文件,其中“foo.properties”是类路径中的资源的绝对路径。

然后你可以将它们注入这样的领域:

@Value("${jdbc.url}") 
private String jdbcUrl; 

其中“jdbc.url”是在你的属性文件引用名。

当然,@Value不会在你的static void main里面工作,但是我真的怀疑static void main是你想要使用你的属性的地方。你应该从Spring Bean中访问它们。

+0

正确,但它的测试应用程序。 –

+0

那么,我会断言在制作Spring测试应用程序时,应该使它成为Spring应该使用的方式。所以你应该测试你可以从Spring组件访问属性,而不是直接从应用程序上下文访问。 [这个问题](http://stackoverflow.com/questions/10822951/how-do-i-get-a-property-value-from-an-applicationcontext-object-not-using-an-a)有一个答案这就解释了为什么在运行时无法从上下文访问属性占位符。 – CorayThan

+0

我也尝试通过在不同的bean中实现MessageSourceAware接口。这也不起作用。 –

0

更改为

<property name="basename" value="message" /> 

在同一个文件夹中message_en_US.properties为您spring.xml

编辑定义MessageSource当你在你的名称的拼写错误。它的名字应该完全是messageSource。由于额外ameassageSourceApplicationContext未能加载它。

+0

OP正在请求readind该文件。 – erencan

+0

@Ravi无法正常工作 –

+0

@JitendraShukla你可以尝试将道具文件移动到'/ src'中吗? –

0

我认为这是从this问题重复。基本上,它与你的包和代码中指定的语言环境之间的不匹配有关。

+0

我尝试过不同的语言环境,没有帮助。 –

0

而不是从ApllicationContext得到消息我从MeassageSource本身得到消息。我改变了我的spring.xml这样

<bean id="employee" class="org.bean.Employee" > 
     <property name="id" value="1"/> 
     <property name="name" value=""/> 
     <property name="dept" value=""/> 
     <property name="messages" ref="messageSource"/>  
     </bean> 

现在我从Employee类调​​用messages.getMessage(this.messages.getMessage("emp", null, Locale.UK))。它的工作。

+0

请检查我的更新。您错误地将bean命名为* meassageSource *(注意额外的“a”)。 –

+0

@RaviThapliyal正如我所说的在实际代码中没有错字 –

0

我已经复制了你的错误,发现了问题。它与Spring没有找到捆绑包有关。我想你应该在发生异常之前发出警告,并发出以下消息:

WARNING: ResourceBundle [resource\message] not found for MessageSource: Can't find bundle for base name resource\message, locale en_US 

这已经是提示。该问题与您的项目结构以及在指定setBasename属性时如何搜索捆绑包有关。请看看this

无论如何,我认为你应该把你的捆绑在更加标准的位置src/main/resources中。如果按照这个约定,你为messageSource豆应该被定义是这样的:

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

用这种方法你例子应该产生所需的线路:

你好员工。

+0

正确,但如果您从'ApplicationContext'获得消息,那么它将不起作用。 –

+0

@JitendraShukla对不起,但它工作。我在测试项目中复制了你的代码,输出是没有错误的预期输出。我从ApplicationContext获取消息。 –