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 ):
ApplicationContext
Interface extendedMessageSource
Api. 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): usedMessageSource
The basic method for obtaining a message. If no message is found in the specified locale, the default message is used. Parameters in args useMessageFormat
To 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,NoSuchMessageException
Exception.
String getMessage(MessageSourceResolvable resolvable, Locale locale)
: All the attributes used in the above method are encapsulated into oneMessageSourceResolvable
Implementation, and this method can be specifiedMessageSourceResolvable
.
WhenApplicationContext
When it is loaded, it will automatically findMessageSource
Type bean. The bean name must bemessageSource
. If found, all calls to the preceding method will be delegated to the bean. OtherwiseApplicationContext
Query whether the parent class contains beans with the same name. If yes, use itMessageSource
. If it does not find any sources, an emptyStaticMessageSource
Will be instantiated so that it can accept the call of the above method.
Spring currently provides twoMessageSource
Implementation:ResourceBundleMessageSource
AndStaticMessageSource
. They all inheritNestingMessageSource
To process nested messages.StaticMessageSource
It 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) definesmessageSource
Bean, through itsbasenames
Attribute references multiple resource files.basenames
Attribute 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.properties
Andwindows.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.properties
Andwindows.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.