Spring Learning (3)---Advanced Topics of Spring Containers

A Spring Container Technology Insider

1 Internal Working Mechanism



For this IOC pipeline, it is not necessary to know too much detail. It is enough to know the general process of Spring from loading the configuration file to creating a complete Bean. If you need to know more about the internal implementation of spring, you can refer to Spring source code analysis. I'll go and read that book sometime later.

2.BeanDefinition
  org, soringframework.beans.factory.config.BeanDefinition is the internal representation of the configuration file <bean> element tag in the container. Under normal circumstances, BeanDefinition is only loaded and parsed when the container is started. Unless the container is refreshed or restarted, the information will not change. Of course, if the user has special needs, the definition of BeanDefinition can be adjusted at runtime programmatically.
  Creating the final BeanDefinition mainly includes two steps
  • 1. Use BeanDefinition to read the configuration information Resource, parse the DOM object of the configuration information through the XML parser, and simply generate the corresponding BeanDefinition object for each <bean>. But the BeanDefinition generated here may be a semi-finished product, because in the configuration file, we may refer to the properties of the external property file through placeholder variables, and these placeholder variables have not been parsed out in this step.
  • Use the BeanFactoryPostProcessor registered in the container to process the BeanDefinition of the semi-finished product, and parse the configuration represented by the placeholder into the final actual value, so that the BeanDefinition of the semi-finished product is the BeanDefinition of the finished product.


3.InstantiationStrategy
org.springframework.beans.factory.support.InstantiationStrategy is responsible for creating a Bean instance based on the BeanDefinition object. The reason why Spring's job of instantiating beans is defied through a strategy interface, so that different instantiation strategies can be adopted to meet different application requirements.

4.BeanWrapper
org.springframework.beansBeanWrapper is an important component class in the Spring framework. BeanWrapper is equivalent to an agent, and Spring completes the filling of Bean properties through BeanWrapper. After the bean instance is created by the InstantiationStrategy, the container master controller wraps the bean instance through the BeanWrapper.
 

Second, the property editor
  In the Spring configuration file, we often provide setting values ​​for various types of Bean properties through literal values: whether it is a double type or an int type, in the configuration file, the literal value of the game character creation type is created. BeanWrapper must have a converter working in it to fill Bean properties, and this converter is the property editor.
  PropertyEditor is an interface defined by the JavaBean specification.

Three
simple
<!-- Introduce jdbc.properties-properties file-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
 p:location="classpath:com/baobaobao/placeholder/jdbc.properties"
 p:fileEncoding="utf-8"/>
<!-- Reference property value by property name-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close"
  p:driverClassName="${driverClassName}"
  p:url="${url}"
  p:username="${username}"
  p:password="${password}"/>


Other properties of PeopertyPlaceholderCConfigurer
 
  • location: If there is only one property file, you can use the location property to specify it directly. If there are multiple property files, you can set it through the locations property, and you can configure the locations like configuring a List.
  • fileEncoding: The encoding format of the property file. Spring uses the default encoding of the operating system to read the property file. If the property file uses a special encoding, it needs to be explicitly specified through this property.
  • order: If multiple PropertyPlaceholderConfigurers are defined in the configuration file, a limited order is specified through this property.
  • placeholderPrefix: In the above example, the property item in the property file is referenced by ${property name}, where "${" is the default placeholder prefix, which can be changed to other prefixes as needed.
  • placeholderSuffix: placeholder suffix, default is "}"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326889553&siteId=291194637