Spring learning: IoC knowledge sorting (2)

Source: Internet
Author: User
Tags i18n

1. BeanFactory or AppliactionContext:

I think you will definitely choose ApplicationContext, which is also recommended by Spring. ApplicationContext itself expands the BeanFactory interface, and BeanFactory cannot be implemented after it is configured for some features, for example, transaction management and AOP. Let's take a look at the features provided by the BeanFactory and ApplicationContext interfaces in the official Spring documents:

From this we can basically see the advantages of ApplicationContext, so it is strongly recommended to use ApplicationContext during development.

2. Use MessageSource for internationalization (i18N ):

ApplicationContextInterface extendedMessageSourceApi. Therefore, the message processing function (i18n or international) is provided ). The MessageSource interface defines the following methods:

  • String getMessage (String code, Object [] args, String default, Locale locale): usedMessageSourceThe basic method for obtaining a message. If no message is found in the specified locale, the default message is used. Parameters in args useMessageFormatTo replace values in messages.
  • String getMessage (String code, Object [] args, Locale loc): essentially the same as the previous method. The difference is that no default value is specified. If no message is found,NoSuchMessageExceptionException.

  • String getMessage(MessageSourceResolvable resolvable, Locale locale): All the attributes used in the above method are encapsulated into oneMessageSourceResolvableImplementation, and this method can be specifiedMessageSourceResolvable.

WhenApplicationContextWhen it is loaded, it will automatically findMessageSourceType bean. The bean name must bemessageSource. If found, all calls to the preceding method will be delegated to the bean. OtherwiseApplicationContextQuery whether the parent class contains beans with the same name. If yes, use itMessageSource. If it does not find any sources, an emptyStaticMessageSourceWill be instantiated so that it can accept the call of the above method.

Spring currently provides twoMessageSourceImplementation:ResourceBundleMessageSourceAndStaticMessageSource. They all inheritNestingMessageSourceTo process nested messages.StaticMessageSourceIt is rarely used, but it can be programmed to add messages to the message source. We basically useResourceBundleMessageSource to instantiate the bean. The following code:

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">        <property name="basenames">            <list>                <value>format</value>                <value>window</value>            </list>        </property>    </bean>

The id attribute is named messageSource and the class is ResourceBundleMessageSource.'applicationContext.xml'(In the root directory of classpath) definesmessageSourceBean, through itsbasenamesAttribute references multiple resource files.basenamesAttribute ValueListThe three values specified by the element are passed in. They exist as files and are placed in the root directory of classpath (format_zh_CN.properties,format_en_US.propertiesAndwindows.properties) (Note: I have used internationalization here, So format defines two properties. In the test class, the corresponding internationalized resources are automatically searched by specifying the attribute set by Locale ).

Three Property Files (format_zh_CN.properties,format_en_US.propertiesAndwindows.properties) The Code is as follows:

# Format_zh_CN.propertiesmessage = hello # format_en_US.propertiesmessage = hello world # window. properties // two placeholders {0} and {1} are used here. When using The placeholder, pass The object [] value to message2 = The arguments {0} {1} is required \!

The test code is as follows:

MessageSource source = new FileSystemXmlApplicationContext ("src/applicationContext. xml "); String message = source. getMessage ("message", null, Locale. US); System. out. println (message );
// Several elements can be stored in the Object [] array. If there are several placeholders in the attribute file, the placeholder value String message2 = source is automatically included in the order. getMessage ("message2", new Object [] {"dataSource", "default"}, "default", null); System. out. println (message2 );

The console output result is as follows:

hello worldThe arguments dataSource default is required!

Because the internationalization is used here, if I set String message = source. getMessage ("message", null, Locale. US); changed to String message = source. getMessage ("message", null, Locale. CHINA);, the console output result is as follows:

 

Hello, The arguments dataSource default is required!

 

I think I should pay attention to this. In fact, the official Spring documentation provides in-depth explanation of the many precautions and features of IoC, which can be used at any time.

 

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.