java – 无法从Controller外部的MessageSource获取消息

前端之家收集整理的这篇文章主要介绍了java – 无法从Controller外部的MessageSource获取消息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当我尝试在@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","BR"));
    }

}

我的组件:

@Component
public class TestComponent {

    @Autowired
    private MessageSource message;

    public void getMessage() {
        //Error
        String message = message.
            getMessage("email.ativacao.title","BR"));
    }

}

我的配置:

 原文链接:https://www.f2er.com/spring/431734.html

猜你在找的Spring相关文章