java 无法从控制器外的 MessageSource 获取消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14626926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get message from MessageSource outside Controller
提问by raonirenosto
Everything works fine when I try to get messages in a @Controller class, but when I try to achieve the same in a @Service or @Component class I receive the following error:
当我尝试在 @Controller 类中获取消息时一切正常,但是当我尝试在 @Service 或 @Component 类中实现相同的消息时,我收到以下错误:
org.springframework.context.NoSuchMessageException:
No message found under code 'email.ativacao.title' for locale 'pt_BR'.
My Controller:
我的控制器:
@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";
}
}
My Service:
我的服务:
@Service
public class TestService {
@Autowired
private MessageSource message;
public void getMessage() {
//Error
String message = message.
getMessage("email.ativacao.title", null, new Locale("pt", "BR"));
}
}
My Component:
我的组件:
@Component
public class TestComponent {
@Autowired
private MessageSource message;
public void getMessage() {
//Error
String message = message.
getMessage("email.ativacao.title", null, new Locale("pt", "BR"));
}
}
My config:
我的配置:
<!-- 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"/>
MessageSource is not null on both @Service and @Component, but they're not able to get the message (Exception above). My properties:
MessageSource 在@Service 和@Component 上都不为空,但它们无法获取消息(上面的异常)。我的属性:
WebContent/WEB-INF/i18n
网页内容/WEB-INF/i18n
- messages_pt_BR
- messages_en_US
- 消息_pt_BR
- messages_zh_CN
I really can't find the problem. Any suggestion to solve this? Thanks.
我实在找不到问题所在。有什么建议可以解决这个问题吗?谢谢。
回答by Kent
From what you were describing, I guess controller bean and messageSource were declared in same context. so then can find each other.
根据您的描述,我猜控制器 bean 和 messageSource 是在相同的上下文中声明的。这样就可以找到对方。
if your service bean and controller bean are not declared in same context, your service cannot find the messageSource.
如果您的服务 bean 和控制器 bean 未在同一上下文中声明,您的服务将无法找到 messageSource。
same context doesn't mean same file. your one.xml could include two.xml.
相同的上下文并不意味着相同的文件。您的 one.xml 可以包含 two.xml。
anyway, if it worked for you, it's good.
无论如何,如果它对你有用,那就太好了。