A good memory is better than a bad pen. 84-spring3 Learning (5)-spring ApplicationContext (Application context)

Source: Internet
Author: User

We generally call beanfactory the IOC container, called ApplicationContext, as the application context (or spring container);
If Beanfactory is the heart of spring, then ApplicationContext is the complete body of spring;
ApplicationContext is derived from beanfactory and provides more practical application-oriented functions.
In Beanfactory, many functions need to be implemented programmatically, while in ApplicationContext they can be configured in the same way.

The ApplicationContext also provides the following features:
1) Messagesource, providing international message access
2) resource access, such as URLs and files
3) Event propagation, the bean that implements the Applicationlistener interface
4) apply multiple above so that each context is focused on a specific level, such as the web layer of the application.

1.MessageSource

The ApplicationContext interface extends the Messagesource interface, thus providing message processing functionality (i18n or internationalization). Used with Hierarchicalmessagesource, it can also handle nested messages, which are the basic interfaces that spring provides for processing messages. Let's take a quick look at the method it defines:
? String GetMessage (String code, object[] args, string default, Locale Loc): The basic method used to get messages from Messagesource. If no message is found in the specified locale, the default message is used. The parameters in args will use the Messageformat in the standard class library as the replacement value in the message.
? String GetMessage (String code, object[] args, Locale Loc): Essentially the same as the previous method, with the difference between: No default value specified, and if no message is found, Throws a Nosuchmessageexception exception.
? String getMessage (messagesourceresolvable resolvable, locale locale): The properties used in the methods above are encapsulated in a messagesourceresolvable implementation, This method can specify the messagesourceresolvable implementation.
When a applicationcontext is loaded, it automatically finds beans in the context that have been defined as Messagesource types. The name of this bean must be messagesource. If found, then all calls to the above method will be delegated to the bean. Otherwise, ApplicationContext will look in its parent class for a bean with the same name. If so, take it as a messagesource. If it does not eventually find any source of the message, an empty staticmessagesource will be instantiated so that it can accept the call of the above method.

ApplicationContext loads multiple configuration files and reads
Configuration of the Bean.xml<beans>  <bean id= "messagesource"class=" Org.springframework.context.support.ResourceBundleMessageSource ">            < property name="Basenames">      <list>        <value>One</value>        <value>Both</value>      </list>    </Property >  </Bean></Beans>

This configuration file tells us how the resource file is loaded, it can load the resources we need from one.properties,two.properties, two files, and they are loaded in the order of the configuration folder.

one.properties# in ‘one.properties‘one=oneformat!two.properties# in ‘two.properties‘message=oneformat!
ApplicationContext Java source code to load multiple configuration files
 Packagecom.spring;ImportOrg.springframework.context.MessageSource;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/** * Simple simulation ApplicationContext load multiple profiles and read properties * * @author Fan Fangming */  Public  class easyapplicationcontext {     Public Static void Main(string[] args) {Messagesource resources =NewClasspathxmlapplicationcontext ("Bean.xml"); String message = Resources.getmessage ("Message",NULL,"Default",NULL);    SYSTEM.OUT.PRINTLN (message); }}

A good memory is better than a bad pen. 84-spring3 Learning (5)-spring ApplicationContext (Application context)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.