Code entry

Before writing the article will be tedious a lot of start again, into the [Spring source code analysis] this plate will directly get to the point.

Many people may want to see the Spring source code, but don’t know how to get started, this is understandable: Java developers usually work on the Java Web. For programmers, when a Web project uses Spring to configure configuration files, the Spring loading process is relatively opaque and it is not easy to find the code entry to load.

Here is a very simple entry to Spring code loading:

1 ApplicationContext ac = new ClassPathXmlApplicationContext(“spring.xml”);

2 ac.getBean(XXX.class);

ClassPathXmlApplicationContext used to load the CLASSPATH of the Spring configuration file, you can see, the second line will have access to the instance of the Bean, so must have already completed the first row of the loading of all the Bean instance, So you can through the ClassPathXmlApplicationContext as entrance. Behind in order to facilitate code reading, gives the ClassPathXmlApplicationContext inheritance of this class:

The ApplicationContext in the lower left corner should have another layer of inheritance. Crucially, it is a subinterface to the BeanFactory.

Finally, the version of Spring used in this article is 3.0.7, which is older and used purely for corporate purposes.

ClassPathXmlApplicationContext store content

In order to understand more ApplicationContext, take an instance ClassPathXmlApplicationContext, for example, look at the content stored inside, to deepen the understanding of ApplicationContext, shown in tabular form:

ClassPathXmlApplicationContext constructor

Look at the constructor of the ClassPathXmlApplicationContext:

1 public ClassPathXmlApplicationContext(String configLocation) throws BeansException {

2 this(new String[] {configLocation}, true, null);

3}

From the second code, three things are done:

1, super (parent)

It doesn’t do much. Set the parent ApplicationContext, null here, okay

2, setConfigLocations (configLocations)

Code will not paste, a look at it, there are two things to do:

(1) Save the specified path of the Spring configuration file to a local directory

(2) in analytic Spring configuration file path ${PlaceHolder} PlaceHolder, replace the PlaceHolder in System variables corresponding to the Value Value, the System itself bring some System variables such as the class. The path, OS. The name, the user. The dir, etc., You can also set your own System variables using the system.setProperty () method

3, the refresh ()

This is the core of the Spring Bean loading, it is the parent class ClassPathXmlApplicationContext AbstractApplicationContext one way, as the name implies, is used to refresh the entire Spring context information, Defines the entire Spring context loading process.

The refresh method

As mentioned above, the refresh() method is at the heart of the entire Spring Bean loading, so take a look at the definition of the entire refresh() method:

The functionality of each submethod will be examined bit by bit later, but first the refresh() method has a few things to learn:

Methods are locked to avoid multiple threads refreshing the Spring context at the same time

Synchronized (startUpShutdownMonitor); synchronized (startUpShutdownMonitor);

(1) The refresh() method and the close() method both use the startUpShutdownMonitor object locking, which ensures that the close() method cannot be called when the refresh() method is called, and vice versa, avoiding collisions

(2) Another benefit that is not covered in this approach is that using object locks reduces the scope of synchronization and only locks blocks of code that cannot be concurrently executed, improving overall code execution efficiency

3. The method uses each submethod to define the entire refresh() method flow, making the whole method flow clear and understandable. This is a good lesson to learn. When dozens or even hundreds of lines of code are written together in a method, it seems to me that there are three obvious problems:

(1) Reduced expansibility. On the other hand, if a process is defined as a method, subclasses can inherit from their parent class and override methods as needed

(2) Poor code readability. It’s very simple. Would someone who reads code want to see 500 lines of code or 10 50 lines of code?

(3) The code maintainability is poor. This point is similar to the above but different. Poor maintainability means that a few hundred lines of code have unclear function points and are not easy to be modified by future generations, which may lead to “affecting the whole body”.

PrepareRefresh method

Here’s a look at the refresh method’s children, starting with the prepareRefresh method, and the source code:

As the name suggests, this method is ready to refresh the Spring context.

1. Set the start time for refreshing the Spring context

2. Set the active flag bit to true

Also notice the log line 12, which prints the Java class that actually loaded the Spring context.

ObtainFreshBeanFactory method

The obtainFreshBeanFactory method gets the Bean factory that refreshes the Spring context as follows:

Its core is the second row refreshBeanFactory method, this is an abstract method, have AbstractRefreshableApplicationContext and GenericApplicationContext these two subclasses implement this method, Have a look at the above ClassPathXmlApplicationContext inheritance diagram that is known, the call should be implemented in AbstractRefreshableApplicationContext refreshBeanFactory, its source is:

This code is the core of line 7, this line DefaultListableBeanFactory this class, the author points out this class is the core of constructing Bean class, the class will be detailed analysis in the next article, first of all give DefaultListableBeanFactory inheritance relationship diagram:

AbstractAutowireCapableBeanFactory the class inheritance hierarchy is deep, the space is limited, there is no continue to draw down, this figure clearly shows the basically DefaultListableBeanFactory hierarchy.

In order to more clearly explain DefaultListableBeanFactory, enumerate the DefaultListableBeanFactory stored in some of the important objects and the contents of the object, DefaultListableBeanFactory basic is operating these objects, in tabular form:

Like can pay attention to forwarding circle of friends oh, thank you, daily update!!