0

フレームワーク: 春 3。

Bean のメッセージ ソース injectend が常に NULL になる理由が本当にわかりません。

スニペットは次のとおりです。

servlet.xml

<context:annotation-config />

<context:component-scan base-package="com.myproject.controllers" />

<mvc:annotation-driven />


<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="/WEB-INF/messages/messages" />
    <property name="cacheSeconds" value="0" />
</bean>

messageSource が注入されるクラス

import com.myproject.controllers.forms.RegistrationForm;

@Component
public class RegistrationFormValidator implements Validator {

@Autowired
@Qualifier("messageSource")
private MessageSource messageSource;


    //other stuff here...

}

ここにコントローラーがあります

@Controller
@SessionAttributes("userSearchForm")
public class UsersController extends PaginationController<ProfiledUser>{


@InitBinder(value="registrationForm")
public void initBinder(WebDataBinder binder)
{
    binder.setValidator(new RegistrationFormValidator());
}

私はすでに次のことを試しました:

  1. 注釈を削除し、xml 構成ファイルを介してメッセージ ソースを挿入する
  2. MessageSourceAware インターフェースの実装
  3. ReloadableresourceBundleMessageSourceインターフェイスを使用する代わりにa を注入しようとしていますMessageSource

すべてが壮大な失敗に終わります;-) MessageSource を適切に注入するにはどうすればよいですか?

4

4 に答える 4

1

CGLIBクラスのフィールド値を調べていると思います

Spring シングルトン Bean フィールドにデータが入力されていない 更新を参照してください

自動配線に関する一般的な注意事項

@AutowiredAutowiredAnnotationBeanPostProcessorアノテーションは、それぞれのSpring構成ファイルでアノテーションを指定することで登録できるによって処理され<context:annotation-config />ます(Beanポストプロセッサーはコンテナーごとに機能するため、サーブレットとアプリケーションルートコンテキストには異なるポストプロセッサーが必要です=><context:annotation-config />両方を配置する必要がありますWeb アプリ コンテキストとディスパッチャー サーブレット構成)。

@Autowired注釈には、デフォルトで true に設定されているプロパティがあることに注意してくださいrequired。つまり、自動配線プロセスが発生した場合、Spring は、指定された Bean のインスタンスが 1 つだけ存在することを確認します。フィールドにアノテーションが付けられた Bean が@Autowiredシングルトンの場合、アプリケーションの初期化中にチェックが実行されます。

updateこの特定の質問ではValidator、Spring によってインスタンスがまったく作成されなかったため、自動配線が実行されず、初期化例外がスローされませんでした。

于 2012-11-24T15:58:14.157 に答える
0

カスタムの Date Formatter で MessageSource を使用しようとしたときに、同様の問題が発生しました。

コード AppDateFormatter

public class AppDateFormatter implements Formatter<Date> {

    @Autowired
    private MessageSource messageSource;

    other stuff.....

    private SimpleDateFormat createDateFormat(final Locale locale) {
        final String format = this.messageSource.getMessage("date.format", null, locale);
        final SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setLenient(false);
       return dateFormat;
   }

}

これは私のために働いたものです:

public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

  other stuff.....

    @Bean
    public MessageSource messageSource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("Messages/Messages", "Messages/Labels");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(1);
        return messageSource;
    }    

    @Bean
    public AppDateFormatter appDateFormatter(){
        return new AppDateFormatter();
    }

    @Bean
    public FormattingConversionService mvcConversionService() {
        FormattingConversionService conversionService = new DefaultFormattingConversionService();
        conversionService.addFormatter(appDateFormatter()); 
        return conversionService;
    }

}
于 2014-10-30T06:39:59.980 に答える
0

ベース名を次のように変更します。

<property name="basename" value="classpath:messages/messages" />
于 2014-03-06T15:03:49.797 に答える
0

Java configs クラスで、次の Bean を追加します。

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");

    return messageSource;
}

@Bean
public LocaleResolver localeResolver() {
    SessionLocaleResolver resolver = new SessionLocaleResolver();
    resolver.setDefaultLocale(Locale.ENGLISH);

    return resolver;
}

@Bean
public MessageSourceAccessor messageSourceAccessor(MessageSource messageSource){
    return new MessageSourceAccessor(messageSource, Locale.ENGLISH );
}

次に、次のようにMessageSourceAccessorBeanを注入できます

@Autowired
private MessageSourceAccessor msgs;

次のようにメッセージ文字列を取得します。

msgs.getMessage("controller.admin.save.success")

message_en.propertiesファイルはフォルダー内にある必要があります/src/main/resources。他の言語にも必要なプロパティ ファイルを追加します。

お役に立てれば。

于 2016-08-18T14:14:44.950 に答える