The interview in these days, I feel that the whole grasp of spring is still very weak. So we need to continue to strengthen.
Here to explain the spring of these several aspects of the test, but the actual feeling is still wrong, this problem I think the need to really read the spring source code to say something to make sense. This kind of interview asks the law, also only is the interview the question law, to the actual grasp still has no slightest meaning. So I think there is a chance to read the spring source to go through the concrete implementation, this is a meaningful approach. Please raise your hand if you agree.
Here's a description of the three spring interview questions:
1. The technology of the IOC container used in the interview of the back-guest technology
The 1th kind of argument:
Reference : Implement simple Spring Ioc yourself
The IOC is a software design pattern that simply means that Spring implements the IOC through Factory + reflection.
The principle of simple explanation:
In fact, by parsing the XML file, we create the beans we need by reflection, and then we put the beans into the collection, and then provide a Getbean () method for us to get the bean.
The 2nd kind of argument:
Reference: 73431021
Key points of knowledge used:
DOM4J parsing an XML file
XPath expressions (used to parse tags in XML)
Java Reflection mechanism
Introspection (Gets the set method of the Bean property to assign value)
The 3rd kind of argument
Reference: Techniques used by spring's Ioc/di
Technology used by the Ioc/di
1. Jdom:jdom is a technique for parsing XML files, and spring's configuration file Applicationcontext.xml is parsed by JDOM, which extracts the tags and attribute values defined in the XML file.
2. Reflection mechanism: The class name in the configuration file using the reflection mechanism can implement class loading initialization and so on, can also invoke the method of the class for attribute injection, Java.lang.reflect provides a reflection-related tool
2: Brief Introduction to the realization principle of beanfactory
Here is the CP code, why CP is because these are the key things I need to remember.
Reference Blog: Simple implementation of spring beanfactory principle
Let's take a look at the Java code to get the code for the Bean in spring (there are five ways to show only one of these methods):
It has not been found that the above code is similar to code that implements Factory mode with reflection. Yes, you're not mistaken, the beanfactory in spring is a simple factory model.
Now the idea is more clear, in order to achieve spring in the beanfactory, but the use of the following several techniques:
1. Use the simple Factory mode to process the bean container.
2. Parse the XML file to get the element information in the configuration.
3. Use reflection to instantiate an object in the configuration information.
4. If there is an object injection, use the Invoke () method.
5. Instantiate the object into the bean container and provide the Getbean method.
Through the above steps to implement the spring beanfactory function, as long as the configuration file configuration, the instantiation of the object to Beanfactory to implement, the user does not need to use the new object to instantiate the object, directly call Getbean method is to get the object instance.
3, Big search car interview spring How to implement the Dependency injection (DI)
Reference blog: http://blog.163.com/[email protected]/blog/static/2292852520091125112112902/
There are three ways to configure dependency injection for a bean in the spring container:
· This is the most common way to inject a setter method that uses attributes;
· Using a constructor injection;
· Use filed injection (for annotation mode).
Thanks to the above blog code-sharing people, there is no time to read the source, can only be remembered first. The interview still needs to be dealt with ~ ~
Back to the technology interview to achieve the IOC container technology, a brief description of the implementation of the principle of beanfactory, big search car interview spring How to implement the Dependency injection (DI)