Why Use ApplicationContext As a MessageSource?

本文探讨了在Spring MVC框架中如何使用ApplicationContext作为MessageSource,分析了其优势及为何优于独立管理MessageSource实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toanswer this question, we need to jump a little ahead of ourselves and look atthe web application support inSpring. The answer, in general, is that you shouldn’t use ApplicationContext asa MessageSource because doingso couples your bean to ApplicationContext unnecessarily (this is discussed inmore detail in the nextsection). You should use ApplicationContext when you are building a webapplication by using Spring’sMVC framework.

 

Thecore interface in Spring MVC is Controller. Unlike frameworks such as Strutsthat require you to implementyour controllers by inheriting from a concrete class, Spring simply requiresthat you implement theController interface (or annotate your controller class with the @Controllerannotation). Having said that,Spring provides a collection of useful base classes that you will use toimplement your own controllers. Each of these base classes is a subclass (directly or indirectly) ofthe ApplicationObjectSupport class, which is a convenient superclass for any application objects thatwant to be aware of ApplicationContext. Remember that in a web application setting, ApplicationContext isloaded automatically.

 

ApplicationObjectSupportaccesses this ApplicationContext, wraps it in a MessageSourceAccessor object, and makes that available to yourcontroller via the protected getMessageSourceAccessor() method. MessageSourceAccessor provides a wide arrayof convenient methods for working with MessageSource instances. Provides a wide array of convenient methodsfor working with MessageSource instances. This form of autoinjection is quite beneficial; it removes the needfor all of your controllers to expose a MessageSource property.

 

However,this is not the best reason for using ApplicationContext as a MessageSource inyour web application. The mainreason to use ApplicationContext rather than a manually defined MessageSource bean is that Spring does, where possible,expose ApplicationContext, as a MessageSource, to the view tier. This means when you are usingSpring’s JSP tag library, the <spring:message> tag automatically reads messages from ApplicationContext, and whenyou are using JSTL, the <fmt:message> tag does the same.

 

Allof these benefits mean that it is better to use the MessageSource support inApplicationContext when you arebuilding a web application, rather than manage an instance of MessageSourceseparately. This is especiallytrue when you consider that all you need to do to take advantage of thisfeature is to configure a MessageSourcebean with the name messageSource.

-----《PRO SPRING 5》

要回答这个问题,我们需要提前一点,看看Spring中的web应用程序支持。通常,答案是不应该将ApplicationContext用作MessageSource,因为这样做会不必要地将您的bean连接到ApplicationContext(这将在下一节中详细讨论)。当您使用SpringMVC框架构建web应用程序时,应该使用ApplicationContext

 

Spring MVC的核心接口是控制器。与需要从具体类继承来实现控制器的Struts等框架不同,Spring只需要实现控制器接口(或者使用@Controller注释您的控制器类)。已经说过,Spring提供了一个有用的基类集合,您将用来实现自己的控制器。这些基类中的每个都是ApplicationObjectSupport类的子类(直接或间接地),对于任何希望了解ApplicationContext的应用程序对象来说,这是一个方便的超类。请记住,在web应用程序设置中,应用程序上下文是自动加载的。

 

ApplicationObjectSupport访问这个ApplicationContext,并将其封装在MessageSourceAccessor对象中,并通过受保护的getMessageSourceAccessor()方法将其提供给控制器。MessageSourceAccessor为使用MessageSource实例提供了大量方便的方法。为使用MessageSource实例提供了大量方便的方法。这种形式的自动注射是非常有益的;它消除了所有控制器公开MessageSource属性的需要。

 

然而,这并不是在web应用程序中使用ApplicationContext作为MessageSource的最佳理由。使用ApplicationContext而不是手动定义的MessageSource bean的主要原因是,在可能的情况下,SpringApplicationContext作为MessageSource向视图层公开。这意味着当您使用SpringJSP标记库时,< Spring:message>标记会自动从。


所有这些好处都意味着,在构建web应用程序时,最好在ApplicationContext中使用MessageSource支持,而不是单独管理MessageSource的实例。当您考虑到要利用这个特性,您需要做的就是使用MessageSource bean配置MessageSource bean时,这一点尤其正确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值