Bromon originality, please respect copyright
The current situation is that many Java programmers must call Spring. Such a large number of programmers collectively call Spring, reflecting the power of the Spring framework. Spring is a good time to call Spring. Let's call Spring together. Pai_^
Spring is designed to simplify J2EE development. So if we still need to crack the White tongue when learning and using it, isn't it a joke? As far as my experience is concerned, spring has done a good job in this aspect. It is indeed a very easy-to-use framework.
I have previously designed a J2EE examination system with a lot of EJB (see http://blog.csdn.net/bromon/archive/2004/08/27/86291.aspx), and I plan to use the same examination system as an example for comparison. The general structure of the two systems is similar, but the new version adopts a lightweight solution. Hibernate is used as the ORM framework, and all objects are handed over to spring for management.
I. IOC and Di
First, I want to talk about IOC (inversion of control, control reversal ). This is the core of spring. For the Spring framework, the so-called IOC is that spring is responsible for controlling the relationship between object lifecycles and objects. What does this mean? For example, how do we find a girlfriend? The common situation is that we go around to see where there is a beautiful and good mm, and then inquire about their interests, QQ number, phone number, IP number, IQ number ........., Find a way to get to know them and give them what they want ...... This process is complex and profound, and we must design and face each link on our own. This is also true for traditional program development. To use another object in an object, you must obtain it (New by yourself or query one from JNDI ), after use, the object will be destroyed (such as connection), and the object will always be combined with other interfaces or classes.
How does IOC work? It's a bit like finding a girlfriend through a matchmaking agency. I introduced a third party between my girlfriend and me: the marriage agency. The matchmaking agency manages a lot of information about men and women. I can submit a list to the matchmaking agency to tell it what kind of girlfriend I want to find, such as Li jiaxin, Lin xilei, and Jay Chou, the speed is like Carlos, the technology is like Zidane, and then the matchmaking agency will provide a mm according to our requirements. We just need to get in love with her and get married. It is simple and clear. If the person selected by the matchmaking agency does not meet the requirements, we will throw an exception. The entire process is no longer controlled by myself, but controlled by a mechanism similar to a container such as matchmaking agency. This is the development method advocated by spring. All classes will be registered in the spring container to tell Spring what you are and what you need, then spring will take the initiative to give you what you want when the system runs properly, and also give you something else you need. The creation and destruction of all classes are controlled by spring. That is to say, the object lifecycle is not referenced but spring. For a specific object, it used to control other objects. Now all objects are controlled by spring, so this is called control inversion. If you still don't understand, I decided to give up.
IOC dynamically provides other objects required by an object during system running. This is achieved through di (dependency injection, dependency injection. For example, object A needs to operate the database. In the past, we always had to write code in a to obtain a connection object. With spring, we only need to tell spring that a connection is required in, as for how to construct the connection and when to construct it, A does not need to know. When the system is running, spring will create a connection at an appropriate time and inject it into a like an injection. This completes the control of the relationship between objects. A needs to rely on the connection to run normally, and the connection is injected into a by spring, so the dependency injection name is like this. How is di implemented? A major feature after Java 1.3 is reflection, which allows programs to dynamically generate objects, execute object methods, and change object attributes during runtime, spring is injected through reflection. For more information about reflection, see Java Doc.
After understanding the concepts of IOC and Di, everything will become simple and clear, and the rest of the work is just accumulating wood in the Spring framework.