2017-02-24 29 views
0

我想從我的控制器發送本地化的電子郵件。Spring MVC:在控制器中的本地化

我的本地化配置和我的Thymeleaf瓷磚工作正常。

我的配置如下:

\t ... 
 
\t <!-- i18n --> 
 
\t <bean id="messageSource" 
 
\t \t class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
 
\t \t <property name="basenames"> 
 
      <list> 
 
       <value>classpath:messages</value> 
 
       <value>classpath:cpcscorereport</value> 
 
      </list> 
 
     </property> 
 
\t \t <property name="defaultEncoding" value="UTF-8" /> 
 
\t </bean> 
 

 

 
<bean id="localeChangeInterceptor" 
 
\t \t class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
 
\t \t <property name="paramName" value="lang" /> 
 
\t </bean> 
 
\t 
 
\t 
 
\t <bean id="handlerMapping" 
 
\t \t class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
 
\t \t <property name="interceptors"> 
 
\t \t \t <ref bean="localeChangeInterceptor" /> 
 
\t \t </property> 
 
\t </bean> 
 

 

 
\t <bean id="localeResolver" 
 
\t \t class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> 
 
\t \t <property name="defaultLocale" value="zh_CN" /> 
 
\t </bean> 
 

 
\t ... 
 

 
</beans>

所以理論上我自動裝配消息源到控制器如下:

@Autowired 
private ReloadableResourceBundleMessageSource messageSource; 

然後按如下方式訪問消息:

 String message = messageSource.getMessage("report.title", null, LocaleContextHolder.getLocale()); 
     SSm.getLogger().debug("localized message: "+message); 

但我得到下面的異常在自動裝配

23-FEB-2017 18:19:06.969嚴重[HTTP-NIO-8080-EXEC-4] org.apache.catalina.core。 StandardWrapperValve.invoke爲Servlet評估分發異常 org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合條件的[org.springframework.context.support.ReloadableResourceBundleMessageSource]類型的符合條件bean:期望至少1個符合自動裝配候選這種依賴性。依賴註解:{@ org.springframework.beans.factory.annotation.Autowired(必需=真)}

爲什麼不拿起控制器的自動裝配Autowired豆?

任何建議將不勝感激。

回答

1

嘗試使用,而不是ReloadableResourceBundleMessageSource

@Autowired 
private MessageSource messageSource; 
+0

完美的MessageSource。我假設ReloadableResourceBundleMessageSource是一個代理或抽象類? – Jake