Java 在Spring3.2中读取属性文件
spring.xmlJava 在Spring3.2中读取属性文件,java,spring,Java,Spring,spring.xml <?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
<?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>
我的属性文件位于src/resource文件夹中。文件名为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.
我喜欢用a来做这个。让你开始
基本上,您需要添加:
<context:property-placeholder location="classpath:foo.properties" />
其中“jdbc.url”是属性文件中的引用名称
当然,@Value
在您的静态void main
中不起作用,但我真的怀疑静态void main
是否是您想要使用属性的地方。您应该从Springbean访问它们。我喜欢使用一个。让你开始
基本上,您需要添加:
<context:property-placeholder location="classpath:foo.properties" />
其中“jdbc.url”是属性文件中的引用名称
当然,@Value
在您的静态void main
中不起作用,但我真的怀疑静态void main
是否是您想要使用属性的地方。您应该从Springbean访问它们。更改为
<property name="basename" value="message" />
使用与您的spring.xml
位于同一文件夹中的message\u en\u US.properties
编辑:定义消息源
时,您的bean名称中有一个输入错误。它的名称应该正好是messageSource
。由于meassageSourceApplicationContext中的额外a
无法加载它。更改为
<property name="basename" value="message" />
使用与您的spring.xml
位于同一文件夹中的message\u en\u US.properties
编辑:定义消息源
时,您的bean名称中有一个输入错误。它的名称应该正好是messageSource
。由于meassageSourceApplicationContext中的额外a
未能加载它。我认为这与问题重复。基本上,这与您的bundle和代码中指定的区域设置之间的不匹配有关。我认为这与问题重复。基本上,这与捆绑包与代码中指定的区域设置不匹配有关。我不是从aplicationcontext
获取消息,而是从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
class调用messages.getMessage(this.messages.getMessage(“emp”,null,Locale.UK))
。它正在工作。我不是从applicationcontext
获取消息,而是从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
class调用messages.getMessage(this.messages.getMessage(“emp”,null,Locale.UK))
。工作正常。我重现了你的错误并发现了问题。这与Spring没有找到捆绑包有关。我认为您应该在异常之前收到警告,并显示以下消息:
WARNING: ResourceBundle [resource\message] not found for MessageSource: Can't find bundle for base name resource\message, locale en_US
这就是暗示。问题与您的项目结构以及指定setBasename属性时如何搜索捆绑包有关。请看一看
无论如何,我认为您应该将包放在更标准的位置src/main/resources。如果遵循此约定,则messageSource bean的定义如下:
@Value( "${jdbc.url}" )
private String jdbcUrl;
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>
使用此方法,您的示例应生成所需的行:
你好,雇员
我重现了你的错误,发现了问题所在。这与Spring没有找到捆绑包有关。我认为您应该在异常之前收到警告,并显示以下消息:
WARNING: ResourceBundle [resource\message] not found for MessageSource: Can't find bundle for base name resource\message, locale en_US
这就是暗示。问题与您的项目结构以及指定setBasename属性时如何搜索捆绑包有关。请看一看
无论如何,我认为您应该将包放在更标准的位置src/main/resources。如果遵循此约定,则messageSource bean的定义如下:
@Value( "${jdbc.url}" )
private String jdbcUrl;
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
</bean>
使用此方法,您的示例应生成所需的行:
你好,雇员
是的,但是它的测试应用程序。好吧,我会断言,当制作一个Spring测试应用程序时,你应该按照Spring制作的方式来制作。因此,您应该测试是否可以从Spring组件而不是直接从应用程序上下文访问属性。有一个答案可以解释为什么在运行时无法从上下文访问属性占位符。我还尝试在不同的bean中实现MessageSourceAware接口。这也不起作用。虽然这是对配置值的一个很好的建议,但它并没有涉及i18n,这是MessageSource
的主要目的。对,但它的测试应用程序。好吧,我要断言,在制作Spring测试应用程序时,你应该按照Spring的方式制作。因此,您应该测试是否可以从Spring组件而不是直接从应用程序上下文访问属性。有一个答案可以解释为什么在运行时无法从上下文访问属性占位符。我还尝试在不同的bean中实现MessageSourceAware接口。这也不起作用。虽然这是一个很好的配置值建议,但它没有解决i18n,MessageSource
@JitendraShukla的主要目的是什么?@JitendraShukla你的豆名有一个输入错误。@JitendraShukla你能尝试将道具文件移到/src
吗?@JitendraShukla你的豆名有一个输入错误。听起来像是你的a