当我尝试在 @Controller 类中获取消息时,一切正常,但是当我尝试在 @Service 或 @Component 类中实现相同时,我收到以下错误:
org.springframework.context.NoSuchMessageException:
No message found under code 'email.ativacao.title' for locale 'pt_BR'.
我的控制器:
@Controller
public class TestController {
@Autowired
TestService service;
@Autowired
TestComponent component;
@Autowired
private MessageSource message;
@RequestMapping(value = "/send", method = RequestMethod.GET)
public String go() {
String message = message.getMessage
("email.ativacao.title", null, new Locale("pt", "BR"));
service.getMessage();
component.getMessage();
return "signsucess";
}
}
我的服务:
@Service
public class TestService {
@Autowired
private MessageSource message;
public void getMessage() {
//Error
String message = message.
getMessage("email.ativacao.title", null, new Locale("pt", "BR"));
}
}
我的组件:
@Component
public class TestComponent {
@Autowired
private MessageSource message;
public void getMessage() {
//Error
String message = message.
getMessage("email.ativacao.title", null, new Locale("pt", "BR"));
}
}
我的配置:
<!-- i18n -->
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.
LocaleChangeInterceptor" p:paramName="lang"/>
</mvc:interceptors>
<!-- Mesage Source Config -->
<bean id="messageSource"
class="org.springframework.context.support.
ReloadableResourceBundleMessageSource" p:fallbackToSystemLocale="true" >
<property name="basename" value="WEB-INF/i18n/messages" />
</bean>
<!-- Mapeia o cookie que irá salvar as opções de idioma -->
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
id="localeResolver" p:cookieName="locale"/>
@Service 和 @Component 上的 MessageSource 都不为空,但它们无法获取消息(上述异常)。我的属性:
网页内容/WEB-INF/i18n
- 消息_pt_BR
- messages_en_US
我真的找不到问题。有什么建议可以解决这个问题吗?谢谢。