In-depth mining of implementation in IOC, AOP, and spring

Source: Internet
Author: User
Tags jboss

Spring features are very powerful. Under its guiding ideology of "never inventing a wheel that you think is good, but just inventing a wheel that you think is not good, by fully practicing the theory of "all seeking truth from facts, 'evidence-based framework'", we have basically integrated Lightweight J2EE application frameworks (such as ORM and MVC, it also constructs some common functions (such as Dao) and forms a powerful J2EE Lightweight enterprise application framework.
However, it may be because you are not fully familiar with spring. Most of the spring functions used in many software enterprises only use their IOC functions, sometimes it comes with the AOP transaction management function.
Although IOC and AOP are not the first in spring, they have done a good job in both sections. It should be said that the entire Spring framework is built around its IOC implementation and AOP implementation. I think it will be very helpful for beginners to thoroughly explore implementation and use in IOC, AOP, and spring.
 
I. Introduction to IOC
IOC-Full name: inversion of control. In addition, IOC is also called di (full name) Dependency injection.
These terms are a bit like learning ancient Chinese. Many Masters also say that there is a famous Hollywood theory in IOC: You stay and don't move, so I will look for you later. You stay, don't move, I will look for you later. Since I have not participated in social practice in Hollywood, it is difficult to understand this sentence.
IOC is a new design model, namely the IOC mode. By introducing IOC containers that implement the IOC mode, the IOC container can be used to manage object lifecycles and dependencies, thus, application configuration and dependency specifications are separated from the actual application code. One feature is to configure the relationship between application components through the text accessory file, instead of modifying and compiling specific Java code.
Currently, the well-known IOC containers include Pico container, avron, spring, JBoss, hivemind, and EJB. In China, the jdon framework of the domestic open-source project is under the responsibility of Banqiao people, it also has the IOC container function (because I didn't have time to carefully study its source code, it seems that the IOC part of jdon is implemented by calling the IOC container function of Pico ).
Among the above several IOC containers, the lightweight ones include Pico container, aveon, spring, hivemind and so on. The super heavyweight ones include EJB, while the half-light and half-heavy ones include JBoss and jdon.
  
What is IOC? How is IOC produced? In what scenarios? Why do we use IOC instead of IOC? "Things have the end and the end of things have the beginning". In order to have a better understanding of this problem, Daxia intends to analyze the development history of its own object-oriented (OO) design and programming, this may allow beginners of IOC to better understand the causes and consequences of the Development of IOC, and strive to "know the truth, know the truth, and make it happen! ".
If you are not in a hurry, you can directly use Baidu's other articles on IOC. Many domestic pioneers have already made a lot of introductions. Such as the "IOC details" of the ice cloud, the design model of Banqiao people and the IOC theory.
 
Ii. Oldest OO programming
I remember when I read think in Java's earliest version, there was an exciting saying: Everything is an object. At this time, the core of our OO programming is centered around three features of object-oriented programming: "inheritance", "encapsulation", and "polymorphism.
2.1 Encapsulation
At that time, we learned to abstract the real things and software models. For example, to describe a cat, the cat should have attributes such as "color", "weight", "male", "temper", and "Birth date, there are also methods such as "run", "eat", "call", and "cat and mouse. As shown in Java code, it is roughly as follows:
Public class cat
{
Private string color; // color
Private string weight; // weight
Private string sex; // The male
Private string temper; // temper
Private string birthday; // Date of birth
Private void run (); // run
Private void eat (Food food); // (food)
Private void shout (INT type); // call (category)
Private Boolean Chase (Mice); // cat and mouse
}
 
2.2 inheritance
In the earliest OO programming period, we also introduced inheritance and often encouraged everyone to continue using it. We thought inheritance was the core of OO programming. The core of inheritance is to abstract parts of classes with common characteristics to the base class. In this way, we can not only use the OO feature, but also reduce the code of many sub-classes.
We know through the common sense of daily life that cats are an animal, so animals have their own characteristics. Therefore, if there are not only cats in our system, there will be many other animals. We will design an animal class and abstract the commonalities of all animals into a base class. Here, the code for cat and animal base classes is roughly as follows:
Public abstract class animal {
Private string color; // color
Private string weight; // weight
Private string sex; // The male
Private string temper; // temper
Private string birthday; // Date of birth
Private void run (); // run
Private void eat (Food food); // (food)
Private void shout (INT type); // call (category)
}
Public class cat extends animal
Private int power; // capability
Private int agility; // agility
// Cat and mouse are unique methods
Private Boolean Chase (Mice ){
Return true;
};
}
 
 
2.3 Polymorphism
At this time, we will also use another feature polymorphism of OO from time to time. Polymorphism is a very important technology, but it is often not well understood and used. Looking back at the previous code, we can see that many places are suspected of being confused.
In this example, we want to write a program to feed pets (including cats, dogs, pigs, leopards, and mice. Using Java's polymorphism, the Code is as follows:
Public class petmanage {
// Feed my pets
Public void feeding (animal)
{
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Animal mypet = new CAT ();
Petmanage PM = new petmanage ();
PM. Feeding (mypet );
}
}
By using the polymorphism feature, if we do not like cats but pigs, we only need to change new CAT () to new pig.
 
2.4 object Lifecycle
In the OO program at this stage, we know that when we want to use an object, we need to use the keyword new in Java to generate one for use. OO for us, everything is so simple, many times even feel that the programming methods of OO and OP are not much different. The Code is as follows:
Cat MyCat = new CAT (); // create a specific cat
MyCat. Shout (); // call
At this time, we trust Java virtualization very much, and our thinking is simple. We know that the life of Java objects begins with the new keyword. We are not very concerned about the end of object life. We know that Java has a garbage collector that is more experienced and intelligent than the C language. It will help us clean the objects that are not used in memory.
Of course, some people do not trust the capabilities of the garbage collector due to their doubts about the loyalty of the garbage collector, therefore, a code similar to "mypet = NULL" is often added in the program to end the object's life.
Of course, we also know that some external resources, such as database connections, need to be familiar with resources manually. Therefore, when using similar resources, you must add the following sentence: conn. Close (), and sometimes Add the following sentence after close (): conn = NULL. Haha, very interesting.
 
Conclusion 2.5
Now it seems that at that time, many naive mistakes were indeed made, and many detours were taken. They did a lot of superfluous work and wrote a lot of hard-to-maintain code.
Compared with today's IOC mode, to find a container similar to spring from the early OO method, it is: "Programmer + JVM itself ". It is used by programmers and JVM to manage object lifecycles and relationships among objects. At that time, if there are any changes, you need to change the code. (although there will be very few modifications to the good design code, you have to change the code too !), Compile the program and run it in the test environment and user environment. So repeatedly, year after day, year after year.
At that time, the inheritance function of OO was the most frequently used code reuse. In addition, there were many op functions.
For some simple source codes involved in this article, please download them from the easyjf open-source team website:
Http://www.easyjf.com/html/bbs/20060602/12718636-1843943.htm? EJB id = 1287314863003738
 
  Coming soon:In the second stage of OO programming, the design pattern is widely used.

(Note: Because I don't want to waste everyone's time, some "confession" is too straightforward. Please forgive me if you don't like spring or those who love spring too much! The "we" in this article only refers to 80 future generations who share the same growth experience as the author. Most of the ideas mentioned in this article are my personal opinions and do not represent anyone.
Author: The copyright of easyjf open-source team Daxia belongs to the easyjf open-source team. You are welcome to repost this article. repost this article to reserve the author's copyright statement. Thank you !)
Appendix
The process and results of playing spring series are occasionally published on the following three websites:
1. easyjf open source team official website, website: http://www.easyjf.com
2. The author's blog, web site: http://www.blogjava.net/daxia/
3. Java Research Organization (JR), URL: http://www.javaresearch.org
You are welcome to join me with more friends and sisters.

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.