Dig into the implementation of IOC, AOP, and spring.

Source: Internet
Author: User
Tags abstract aop inheritance jboss
The function of spring is very powerful, under the guiding ideology of "never invent a wheel that you think is good, but only invent a wheel that you think is bad", by fully practicing the theory of "everything is realistic and the working mode of the evidence structure", we basically put the lightweight Java EE application framework (such as ORM, MVC, and so on, has been consolidated and framed a number of commonly used functions (such as DAO) to form a powerful Java EE Lightweight Enterprise application framework.
However, it may be that spring is not well understood, and the spring features used in many software companies are mostly used only for their IOC features, sometimes with AOP transaction management capabilities.
IOC and AOP, though not the spring initiative, are doing well in both, and it should be said that the entire spring framework is built around its IOC implementation and AOP implementations. I think digging into the IOC, AOP, and implementation in spring, and so on, can be very helpful for beginners.

I. Introduction of the IOC
ioc-full name inversion of control, Chinese explanation: controlled inversion. In addition, the IOC is also called Di (full name) Dependency injection, Chinese explanation: Dependency Injection.
Oh, these nouns make a little like to learn the taste of ancient prose ah. Many masters also say there is a famous Hollywood theory in the IOC: You stay still, and I'll find you. You stay still, and I'll call you when I get there. Since I have not been to Hollywood to participate in social practice, so this sentence to understand a little difficult.
IOC is a new design pattern, the IOC model, in which the IOC container can be used to manage the lifecycle of objects, dependencies, etc. by introducing the IOC container, which makes the application's configuration and dependency specification separate from the actual application code. One of the features is the configuration of the relationship between application components through the text accessories file without having to modify and compile the specific Java code.
At present, the more well-known IOC containers are: Pico Container, Avalon, Spring, JBoss, Hivemind, EJB, etc., domestically owned by Banqiao people responsible for the domestic open source project Jdon framework, but also has the IOC container function (due to the lack of time to carefully read its source code , it seems that the IOC part of Jdon is called Pico's IOC container function.
In the above IOC containers, lightweight with Pico Container, Avalon, Spring, Hivemind, super Heavyweight EJB, and half light and half heavy with a container with Jboss,jdon.
  
What the IOC really is. How did the IOC come into being? What is the occasion? Why do we not use IOC before, and now we need IOC. In order to understand this problem more thoroughly, "Things have always been done and everything has always begun". Big Gorge intends to analyze from its own understanding of object-oriented (OO) design and programming development process, perhaps this can let the IOC beginners know more about the development of the IOC's cause and the cause, strive to do "know it, know why, make it!".
If you can not wait, directly Baidu about the IOC's other articles, the domestic many pioneers have made a lot of introductions. such as the ice cloud of "the IOC detailed", Banqiao's design model and the IOC theory.

Second, the oldest OO programming
Remember when you saw the earliest version of "Think in Java", there was such a rousing remark: Everything is an object. At this point, the core of OO programming is centered around the three features of object-oriented programming, namely "Inheritance", "encapsulation", "polymorphism".
2.1 Package
At that time we learned to abstract the things and software models of reality. For example, to describe a cat, then this cat should have "color", "weight", "male Mother", "temper", "date of birth" and other attributes, there are "run", "Eat", "call", "Cat Catch Mice" and other methods. As the Java code indicates, this is roughly the following:
Public Class Cat
{
Private String color;//Color
Private String weight;//Weight
Private String sex;//Male mother
Private String temper;//Temper
Private String birthday;//Date of birth
private void Run ()/Run
private void Eat (Food Food); Eat (food)
private void shout (int type);//called (category)
Private Boolean Chase (Mice Mice);//cat Catch mouse
}

2.2 Inheritance
In the earliest OO programming period, we will also introduce inheritance, and often encourage people to continue, that inheritance is the core of OO programming ideas. The core of inheritance revolves around how to abstract a part of a class with a common attribute between classes into a base class. It is not only possible to use OO features, but also to reduce the number of subclass code.
We know from the common sense of everyday life that cats are a kind of animal, so the characteristics of animals have almost all of them. So, if we have a cat in our system, there will be many other animals. We will design an animal class that abstracts all the animals ' commonalities into a base class. Here, the code for the cat and animal base class is as follows:
Public abstract Class animal{
Private String color;//Color
Private String weight;//Weight
Private String sex;//Male mother
Private String temper;//Temper
Private String birthday;//Date of birth
private void Run ()/Run
private void Eat (Food Food); Eat (food)
private void shout (int type);//called (category)
}
Public Class Cat extends Animal
private int power;//Ability
private int agility;//Agility degree
Cat and Mouse is a unique method.
Private Boolean Chase (Mice Mice) {
return true;
};
}


2.3 polymorphic
At this point we will use another feature polymorphism from OO. Polymorphism is a very important technology, but many times there is no good understanding and use, looking back at the previous code, we see a lot of places belong to the suspicion of the mysterious.
Take the example above, if we are going to write a program that feeds pets (cats, dogs, pigs, leopards, mice, etc.). Using Java's polymorphic features, our approximate code is as follows:
public class Petmanage {
Feed My pet.
public void feeding (Animal a)
{
}
/**
* @param args
*/
public static void Main (string[] args) {
Animal mypet=new Cat ();
Petmanage pm=new petmanage ();
Pm.feeding (Mypet);
}
}
By using polymorphic characteristics, which day if we do not like cats, but like to pig, as long as the New Cat () into the new Pig (), you can.

2.4 Object life cycle
In this phase of OO programs, we know that when we want to use an object, we need to use the keyword new in Java to generate one. Oo for us, everything is so simple, many times even feel oo and OP's programming method is not much different. The code is as follows:
Cat Mycat=new Cat ()//create a specific cat
Mycat.shout ()/Call a sound
At this point, we are very trusting of Java virtualization, our thinking is very simple, we know that the life of Java objects began in the New keyword. We don't care about the end of the object's life, we know that Java has a more than C language, intelligent garbage collector, he will help us clean up the memory of the unused objects.
Of course, some people because of the garbage collector loyalty doubt, do not trust the garbage collector's ability, so in the program often add a similar "Mypet=null" code to end the object of life.
Of course, we also know that there are some external resources such as database connections, which require manual familiarity with resources. So I know that when I use a similar resource, I have to add one sentence: Conn.close (), and sometimes add a sentence after close (): Conn=null. Oh, very interesting.

2.5 Summary
Now it seems, in fact, at that time did make a lot of childish mistakes, but also walked a lot of detours, do a lot of the work of the superfluous, wrote a lot of difficult and maintenance code.
In contrast to today's IOC model, to find a container similar to spring in the early OO approach, it is: "Programmer +JVM itself." It is the programmer and the JVM that we manage the lifecycle of objects, the relationships between objects, and so on. At that time, if there are any changes need to change the code, (although good design code changes will be very small, but also have to change.) And then compiles and then gets executed in the test environment and the user environment. So again and again, day after day, years after year.
At that time, our code reuse was the most used for OO inheritance, as well as many of the functions that came with the OP method.
This article refers to a few simple source code, please go to EASYJF open Source Team website Download, address:
http://www.easyjf.com/html/bbs/20060602/12718636-1843943.htm?ejid=1287314863003738

   Next Period Notice:The second phase of OO programming, the wide application of design patterns.

(remark: Because the author does not want to beat around the bush wasting time, some "vindicate" unavoidably too direct, also please do not like spring or too much like spring's peers forgive! In this article, "we" only refers to the author has the same growth experience of the people, for the point mentioned in the article, Most of them belong to the author's personal opinion, not to anyone.
This article author: EASYJF Open source Team Great Gorge Copyright EASYJF Open Source Team All, welcome reprint, reprint please retain author copyright notice, thank you. )
Report
As for the play Spring series, the process and results will be published irregularly on the following three websites globally:
1.EasyJF Open Source Team official website, URL: http://www.easyjf.com
2. The author's blog, url: http://www.blogjava.net/daxia/
3.Java Research Organization (JR), Web site: http://www.javaresearch.org
Welcome to more like my brother and sister like to play together.

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.