Dig deeper into IOC, AOP, and implementation in spring.

Source: Internet
Author: User
Tags abstract aop inheritance jboss
Spring's function is very powerful, in its "never invented the wheel that they think good, and only invented the wheel that they think bad" under the guiding ideology, through the full practice of "all truth-seeking, ' evidence-based architecture ' work style" theory, basically put the lightweight Java EE application framework (such as ORM, MVC, etc.), and to frame a number of common functions (such as DAO), form a powerful Java EE Lightweight Enterprise application framework.
However, it may be that the spring is not well mastered by everyone, see many software companies in the use of spring functionality, mostly only with its IOC function, sometimes with the use of the AOP transaction management functions.
Although IOC and AOP are not spring firsts, they are doing well in both of these pieces, and it should be said that the entire spring framework is built around its IOC implementation and AOP implementations. I think digging deeper into IOC, AOP, and implementation in spring, using, etc., 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 called Di (full name) Dependency injection, Chinese explanation: Dependency Injection.
Oh, these nouns make a bit like learning the taste of ancient Chinese ah. Many masters also say that there is a famous Hollywood theory in the IOC: Stay still, and I'll find you. You stay still, I'll call you when I get there. Because I have not been to Hollywood to participate in social practice, therefore, this sentence understanding a little difficult.
IOC is a new design pattern, the IOC model, in which the IOC container can be used to manage the life cycle, dependencies, etc. of the object by introducing an IOC container that implements the IOC schema, which separates the application configuration and dependency specifications from the actual application code. One feature is the configuration of the interaction between application components through the text's accessory file without having to re-modify and compile the specific Java code.
Currently more well-known IOC containers are: Pico Container, Avalon, Spring, JBoss, Hivemind, EJB, etc., domestic by the Banqiao people responsible for the domestic open source project Jdon framework, but also has the IOC container function (because no time to seriously study its source , it seems that the IOC part of Jdon is implemented by invoking the IOC container function of Pico.
In the above several IOC containers, the lightweight has Pico Container, Avalon, Spring, Hivemind, and so on, super-heavyweight with EJB, and semi-light semi-heavy container has Jboss,jdon and so on.
  
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. "The thing has outs, the matter has the final beginning", in order to understand this question more thoroughly, the big Gorge intends to carry on the analysis from oneself understands the object-oriented (OO) design and the programming development course, perhaps this can let the IOC beginner understand the IOC's development the cause and consequences, strives for "knows it, knows its why, makes it!
If we are not in a hurry, we will direct Baidu about the IOC's other articles, many of the domestic pioneers have made a lot of introductions. such as the "IOC details" of the ice cloud, the design model of Banqiao and the IOC theory.

Second, the oldest OO programming
Remember to watch "Think in Java," the earliest version of the time, there is such an exciting words: everything is the object. At this point, the core of OO programming revolves around the three features of object-oriented programming, namely "Inheritance", "encapsulation" and "polymorphism".
2.1 Package
At that time we learned to abstract the real thing and the software model. For example, to describe a cat, then the cat should have "color", "weight", "male and female", "temper", "date of birth" and other properties, there are "run", "Eat", "called", "cat and Mouse" and other methods. As shown in Java code, it is roughly the following:
Public Class Cat
{
Private String color;//Color
Private String weight;//Weight
Private String sex;//Male and female
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 catches mice
}

2.2 Inheritance
The earliest OO programming period, we will also introduce inheritance, but also often encourage people to continue to use, that inheritance is the core of OO programming ideas. The core of inheritance is the abstraction of a common attribute between classes and classes into a base class. It is thought that this will not only use the OO features, but also reduce the code of many sub-classes.
We know by common sense that the cat is an animal, so the animal has a characteristic that he basically has. So, if we have cats in our system, there will be a lot of other animals appearing. We will design an animal class that abstracts the generality of all animals into a single base class. Here, the code for the cat and animal base class is roughly as follows:
Public abstract Class animal{
Private String color;//Color
Private String weight;//Weight
Private String sex;//Male and female
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;//capability
private int agility;//Agility
Cat and mouse are unique methods
Private Boolean chase (mice mice) {
return true;
};
}


2.3 polymorphic
At this point we will occasionally use another feature polymorphism of OO. Polymorphism is a very important technology, but many times it is not well understood and used, looking back to 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 {
To 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 features, one day if we don't like cats, but when we like to raise pigs, just turn new Cat () into new Pig ().

2.4 Object life cycle
In this phase of the OO program, we know that to use an object, we need to use the Java keyword New to generate a to use. Oo for us, everything is so simple, many times even feel oo and op 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 virtual, and our thinking is very simple, we know that the life of the Java object begins with the new keyword. We don't care much about the end of the object's life, we know that Java has a more intelligent garbage collector than the C language, and he will help us clean up objects that are not in memory.
Of course, some people because of the garbage collector loyalty to doubt, not rest assured the ability of the garbage collector, so in the program often add a similar "Mypet=null" code to end the life of the object.
Of course, we also know that there are some external resources, such as database connections, that require manual familiarity with resources. Then know that in the use of similar resources must be added a sentence: Conn.close (), and sometimes in close () to add a sentence: Conn=null. Oh, very interesting.

2.5 Summary
Now it seems that, in fact, it did make a lot of childish mistakes, but also took a lot of detours, did a lot of work to the Lily, wrote a lot of difficult and maintenance code.
In contrast to today's IOC model, to be hard-looking for a spring-like container from the early OO approach, it is: "Programmer +JVM itself". It is the programmer and the JVM that together we manage the life cycle of objects, the relationships between objects, and so on. At that time, if there were any changes, the code would need to be changed (although a good design code would be very small, it would have to be changed.) ), then compile, and then get to the test environment and execute it in the user's environment. So again and again, day after day, years.
At that time, the most used code reuse was the OO inheritance function, and there are many functions brought in the OP method.
This article involves a few simple source code, please go to EASYJF Open Source team official website to download, address:
http://www.easyjf.com/html/bbs/20060602/12718636-1843943.htm?ejid=1287314863003738

   Next Issue preview:The second stage of OO programming, the wide application of design patterns.

(Note: Because I do not want to beat around the bush to waste time, some "confession" inevitably too direct, but also do not like spring or too much like spring counterparts more forgive me! "we" in this article refers to the author has the same growth experience of Gen Y people, for the article mentioned in the view, Most of them belong to the author's personal opinion and not to anyone.
This article author: EASYJF Open source Team big gap Copyright EASYJF Open Source Team All, welcome reprint, reproduced please retain the author's copyright notice, thank you. )
Report
On the "Play Spring Series", the process and results of the play will be on the following three sites around the world in a synchronized publication:
1.EasyJF Open Source Team official website, address: http://www.easyjf.com
2. Author's blog, Website: http://www.blogjava.net/daxia/
3.Java Research Organization (JR), website: http://www.javaresearch.org
Welcome to play with my brother, sister, brother and sister as much as I love playing.

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.