Playing with Spring IoC (1)

Source: Internet
Author: User
Tags jboss
Spring is very powerful, in its" Never invent the wheel that you think is good, but the wheel that you think is bad."Under the guiding ideology, through the full practice of" All practical and evidence-based architecture work methodsThe theory basically integrates the Lightweight J2EE application framework (such as ORM and MVC) and constructs some common functions (such as Dao ), formed 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 want to explore the implementation and usage of IOC, AOP, and spring in depth, which will be of great help to beginners. Therefore, starting from this issue, daxia's Spring Series will bring you into the world of IOC and AOP.
Due to my limited level, there may inevitably be many shortcomings or even errors in this article. Please leave your comments blank. 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. 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 ).
In the preceding IOC containers, Lightweight include Pico container, avron, spring, hivemind, etc. super heavyweight include EJB, while semi-light and semi-heavy 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 things have the end"To better understand this issue, Daxia intends to analyze the object-oriented (OO) design and programming development process it understands, this may allow beginners of IOC to better understand the causes and consequences of IOC development and strive to" Knowing, knowing why, and making 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 programmingI 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 OO programming is the 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 the 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 LifecycleIn 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 (); // at this point, we trust Java virtualization very much, and our thoughts are 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, if you want 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. This article involves a few simple source code, please download to the easyjf open source team official website, address: 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: easyjf open-source team Daxia is welcome to repost this article. repost this article to keep the author's statement. Thank you !)

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.