Spring Introductory--spring Framework underlying principle

Source: Internet
Author: User

As we can see from the previous blog, spring can maintain individual beans (objects) and inject attribute values into them. So, what if we're going to inject an object's reference into another object? What should I do with it?

We know that for properties in an object, we inject properties by using the property tag in the configuration file.

<property name= "object property name" >        <value> What to inject </value> </property>

So what if we have a reference to another object in our class? This time, in fact, is similar to injecting a property, just need to use the ref tag for injection: First we need to configure two classes through the bean, we give their ID named: Class 1, Class 2. At this point, if our requirement is that Class 1 contains a reference to Class 2. In other words, Class 2 is a property of Class 1. Then we need to inject the object of Class 2 into the Class 1 object. The wording in the configuration file is as follows:

<bean id= "Class 1" class= "Class 1 full path" >       <!--Here The injection method is similar to the above value injection, can be written on one line can also be multiple lines--        <property name= " Class 2 in Class 1 property name "Ref=" Class 2 "></bean> <!--the class 2 here is the object name, and when injected, the value in ref must be exactly the same as the ID behind the bean--><bean Id=" Class 2 "class=" Full path for Class 2 "></bean>

The above approach is that our spring manages the individual objects and maintains the relationships between the individual objects. The specific implementation principle is:

The Spring container object is created when Classpathxmlapplicationcontext ("Applicationcontext.xml") executes the sentence. This object reads the bean that is configured in Applicationcontext.xml and is created and put into memory. How exactly did you create it? Depends on our reflection mechanism.

Take the above code: first Class 1 is created, after creation, assuming that the object in memory address is: 123 object in the reference to object two is (???). )。 The memory at this time is as follows:


Then Class 2 is created, and the memory address of Class 2 in the object is assigned a reference to the corresponding field in Class 1. Specifically: Assuming that the address of Class 2 is: 456 then the 123 object of the reference to Object 2 by (??? ) instead (456) to complete the dependency injection of the class. At this point the memory is as follows:


In fact, these objects all exist in a hashmap-like reference to the ApplicationContext object. This is why we want to set the ApplicationContext object to a singleton.

So, how does spring implement just those calls? First, by reading our configuration file through dom4j, we can parse the full path of all related classes. It then uses the reflection mechanism to complete the instantiation of the class by using the following code: Class 1=class.forname ("Full Path to Class 1"). At this point, we get the Class 1. (This is also why classnotfind errors occur when our class's full path is incorrectly written.) )

When we get Class 1, we inject the property to the object by invoking the set method of Class 1. Also, you need to follow the set specification for the first letter of capitalization. For example, if the property of a field in our class is name, then the set method must be written as SetName (the first letter of the name is capitalized) or an error is reported that the property cannot find:

1 public   void SetName (String name) {2              this.name= name; 3   

After the object is created, we put our object ID and our object's physical address together in a container similar to HashMap, and then how do we get the objects we need and then execute the methods in the object? Through the method of Getbean, we get the object's physical address through the object ID, get the object, then call the method of the object, complete the call to the method.

Spring Introductory--spring Framework underlying principle

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.