0

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
           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/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.
4

5 に答える 5

6

そのためにPropertySourcesPlaceholderConfigurerを使用するのが好きです。これは、開始するための優れたチュートリアルです。

基本的に、次を追加します。

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

「foo.properties」はクラスパス内のリソースの絶対パスです。

次に、次のようにフィールドに挿入できます。

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

「jdbc.url」は、プロパティ ファイル内の参照名です。

もちろん、 は@Valueあなたの 内では機能しませんが、とにかくプロパティをどこで使用したいかはstatic void main本当に疑問です。static void mainSpring Bean からそれらにアクセスする必要があります。

于 2013-07-28T20:44:36.500 に答える
0

これはこの質問から複製されていると思います。基本的に、バンドルとコードで指定されたロケールとの不一致に関係しています。

于 2013-07-28T22:07:00.973 に答える
0

私はあなたのエラーを再現し、問題を発見しました。これは、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 は次のように定義する必要があります。

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

このアプローチでは、あなたの例は目的の行を生成するはずです:

こんにちは従業員。

于 2013-07-29T16:48:39.150 に答える
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>

今、私はクラスmessages.getMessage(this.messages.getMessage("emp", null, Locale.UK))から電話しています。Employeeその作業。

于 2013-07-29T11:04:10.427 に答える
0

への変更

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

message_en_US.properties同じフォルダにありますspring.xml

編集:を定義するときに、 Bean名にタイプミスがありますMessageSource。その名前は正確にmessageSource. meassageSourceaのその余分なため、それをロードできませんでした。 ApplicationContext

于 2013-07-28T20:50:40.187 に答える