What is IOC?

Source: Internet
Author: User
Keywords: Spring IoC (control reversal)

Struts is a web layer framework that implements the MVC mode. Spring is an application layer framework. Its core function is to use IOC to separate configuration code from functional code. Hibernate is used to implement or ing.

Recently, the team lead asked every two of us to learn one technology and let me and another team member learn spring. I read some materials and learned that spring is an Aspect-oriented programming (AOP) and IOC containers.
So what is IOC? I found a very interesting explanation on the Internet.
IOC is the inversion of control, and the control is reversed. In Java Development, IOC means that the classes you have designed are handed over to the system for control, rather than being controlled within your class. This is called control inversion.

Here are a few examples to illustrate what IOC is.

Suppose we want to design a girl and a boy class, where girl has a kiss method, that is, girl wants to kiss a boy. So our question is, how can girl know this boy?

In China, common methods of understanding mm and GG are as follows:
1. qingmei bamboo; 2. Parents and Friends; 3. Parents
Which one is the best?

Childhood girl knows her own boy.

Public class girl {
Void kiss (){
Boy boy = new boy ();
}
}

However, the disadvantage of creating a boy from the beginning is that it cannot be changed. And is responsible for the entire life cycle of boy. What if our girl wants to change one? (In severe cases, it is not supported for girls to change boy frequently ,#_#)

Introduction to friends and friends: the intermediary is responsible for providing boys to meet

Public class girl {
Void kiss (){
Boy boy = boyfactory. createboy ();
}
}

It is good to introduce friends and friends. If you are not satisfied, try another one. However, friends and friends boyfactory often appear in the form of singleton. Otherwise, it exists in globals and is everywhere. It is too tedious and inflexible. Why do I need such friends and friends to be involved? Why must I pay her a referral fee? What if my best friend falls in love with my boyfriend?

Parents: give everything to parents. You don't have to worry about it. You just have to wait for Kiss.

Public class girl {
Void kiss (boy ){
// Kiss boy
Boy. Kiss ();
}
}

Well, this is the best method for girl, as long as you want to bribe her parents and give the boy to him. Then we can easily kiss with girl. It seems that the fate of traditional parents for thousands of years is really useful. At least boys and girls don't have to worry about themselves.

This is IOC, which extracts object creation and retrieval to the external. The required components are provided by external containers.

We know Hollywood principles: "Do not call us, we will call you." means you, girlie, do not call the boy. We will feed you a boy.

We should also know that the dependency inversion principle is dependence inversion princinple, dip

Eric Gamma says abstract programming is needed. Object-Oriented Programming is the core of object-oriented programming.

The component is divided into two parts: Service, the declared Implementation of the provided functions, and the implementation of the service

The advantage is that multiple implementations can be switched at will to prevent the problem of "Everything depends on everything", that is, the specifics depend on the specifics.

Therefore, our boy should implement the kissable interface. In this way, if the girl doesn't want to kiss the nasty boy, she can kiss the cute kitten and the kind grandmother.
Ii. IOC type

The type of IOC refers to several different ways for girl to get Boy. Let's explain it one by one.

IOC Type 0: No IOC
Public class girl implements servicable {
Private kissable;
Public girl (){
Kissable = new boy ();
}
Public void kissyourkissable (){
Kissable. Kiss ();
}
}

Girl builds her own boy, which is difficult to change and shared with others. She can only use it independently and is responsible for the full life cycle.

IOC Type 1, first look at the code: Code

Public class girl implements servicable {

Kissable;

Public void Service (servicemanager MGR ){
Kissable = (kissable) Mgr. Lookup ("kissable ");
}

Public void kissyourkissable (){
Kissable. Kiss ();
}
}

This situation occurs in aveon framework. When a component implements the servicable interface, it must implement the service method and pass in a servicemanager. It contains other required components. You only need to initialize the desired boy in the service method.

In addition, the object obtained from context in J2EE also belongs to type 1. It depends on the configuration file.

IOC type 2:

Public class girl {

Private kissable;

Public void setkissable (kissable ){
This. kissable = kissable;
}

Public void kissyourkissable (){
Kissable. Kiss ();
}
}

Type 2 appears in Spring framework. It uses the Set Method of Javabean to pass the required boy to girl. It must depend on the configuration file.

IOC Type 3:

Public class girl {

Private kissable;

Public girl (kissable ){
This. kissable = kissable;
}

Public void kissyourkissable (){
Kissable. Kiss ();
}
}

This is the component of picocontainer. Pass boy to girl through the constructor

Picocontainer Container = new defaultpicocontainer ();
Container. registercomponentimplementation (boy. Class );
Container. registercomponentimplementation (girl. Class );
Girl girl = (girl) container. getcomponentinstance (girl. Class );
Girl. kissyourkissable ();

References

1 http://www.picocontainer.org/presentations/JavaPolis2003.ppt
Http://www.picocontainer.org/presentations/JavaPolis2003.pdf

2 dip, Robert c Martin, Uncle Bob's excellent papers
Http://www.objectmentor.com/resources/articles/dip.pdf

3 dependency injection dependent injection, matrin Fowler extension of dip
Http://www.martinfowler.com/articles/injection.html

4 IOC framework

Picocontainer excellent IOC framework
Http://picocontainer.org/

Aveon
Http://avalon.apache.org/

Spring framework
Http://www.springframework.org/

Hivemind
Http://jakarta.apache.org/commons/hivemind

Struts is a web layer framework that implements the MVC mode. Spring is an application layer framework. Its core function is to use IOC to separate configuration code from functional code. Hibernate is used to implement or ing.

What is IOC?

IOC is the inversion of control, and the control is reversed. In Java Development, IOC means that the classes you have designed are handed over to the system for control, rather than being controlled within your class. This is called control inversion.

Use an example to describe what is IOC

Suppose we want to design a girl and a boy class, where girl has a kiss method, that is, girl wants to kiss a boy. So our question is, how can girl know this boy?

In China, common methods of understanding mm and GG are as follows:

1. qingmei bamboo; 2. Introduction to friends and friends;
3. Parents

Which one is the best?

Childhood girl knows her own boy.

Public class girl {
Void kiss (){
Boy boy = new boy ();
}
}

 

However, the disadvantage of creating a boy from the beginning is that it cannot be changed. And is responsible for the entire life cycle of boy. What if our girl wants to change one? (In severe cases, it is not supported that girls often change boys)

Introduction to friends and friends: the intermediary is responsible for providing boys to meet

Public class girl {
Void kiss (){
Boy boy = boyfactory. createboy ();
}
}

 

It is good to introduce friends and friends. If you are not satisfied, try another one. However, friends and friends boyfactory often appear in the form of singleton. Otherwise, it exists in globals and is everywhere. It is too tedious and inflexible. Why do I need such friends and friends to be involved? Why must I pay her a referral fee? What if my best friend falls in love with my boyfriend?

Parents: give everything to parents. You don't have to worry about it. You just have to wait for Kiss.

Public class girl {
Void kiss (boy ){
// Kiss boy
Boy. Kiss ();
}
}

Well, this is the best method for girl, as long as you want to bribe her parents and give the boy to him. Then we can easily kiss with girl. It seems that the fate of traditional parents for thousands of years is really useful. At least boys and girls don't have to worry about themselves.

This is IOC, which extracts object creation and retrieval to the external. The required components are provided by external containers.

Hollywood principles: "Do not call us, we will call you." means you, girlie, do not call the boy. We will feed you a boy.

 

We should also know that the dependency inversion principle is dependence inversion princinple and dip.
Eric Gamma says abstract programming is needed. Object-Oriented Programming is the core of object-oriented programming.

Components are divided into two parts:

Service.

Implementation, service implementation

The advantage is that multiple implementations can be switched at will to prevent the problem of "Everything depends on everything", that is, the specifics depend on the specifics.

Therefore, our boy should implement the kissable interface. In this way, if the girl doesn't want to kiss the nasty boy, she can kiss the cute kitten and the kind grandmother.

Ii. IOC type

The type of IOC refers to several different ways for girl to get Boy. Let's explain it one by one.

 

IOC Type 0: No IOC

 

Public class girl implements servicable {

Private kissable;

Public girl (){
Kissable = new boy ();
}

Public void kissyourkissable (){
Kissable. Kiss ();
}

}

 

Girl builds her own boy, which is difficult to change and shared with others. She can only use it independently and is responsible for the full life cycle.

 

IOC Type 1, first look at the Code:

 

Public class girl implements servicable {

Kissable;

Public void Service (servicemanager MGR ){
Kissable = (kissable) Mgr. Lookup ("kissable ");
}

Public void kissyourkissable (){
Kissable. Kiss ();
}

}

 

This situation occurs in aveon framework. When a component implements the servicable interface, it must implement the service method and pass in a servicemanager. It contains other required components. You only need to initialize the desired boy in the service method.

In addition, the object obtained from context in J2EE also belongs to type 1.

It depends on the configuration file

<Container>
<Component name = "kissable" class = "boy">
<Configuration>... </Configuration>
</Component>

<Component name = "girl" class = "girl"/>
</Container>

 

IOC type 2:

Public class girl {

Private kissable;

Public void setkissable (kissable ){
This. kissable = kissable;
}

Public void kissyourkissable (){
Kissable. Kiss ();
}

}

Type 2 appears in Spring framework. It uses the Set Method of Javabean to pass the required boy to girl. It must depend on the configuration file.

<Beans>
<Bean id = "boy" class = "boy"/>
<Bean id = "girl" class = "girl">
<Property name = "kissable">
<Ref bean = "boy"/>
</Property>
</Bean>
</Beans>

 

IOC Type 3

Public class girl {

Private kissable;

Public girl (kissable ){
This. kissable = kissable;
}

Public void kissyourkissable (){
Kissable. Kiss ();
}

}

This is the component of picocontainer. Pass boy to girl through the constructor.

Picocontainer Container = new defaultpicocontainer ();
Container. registercomponentimplementation (boy. Class );
Container. registercomponentimplementation (girl. Class );
Girl girl = (girl) container. getcomponentinstance (girl. Class );
Girl. kissyourkissable ();

 

 

 

 

Or ing (ORM)
O = Object
R = relation
M = Mapping

It can be seen from the literal meaning that the JavaBean value object (JavaBean value object) is mapped to a link (data table). The key is the ing. The ing framework is used to implement the ing ), what frameworks can be used? Hibernate, ibatis, etc.

What is the process? Write different ing files based on different ORM frameworks and store them in XML format.
One-to-one correspondence between the table and the value objects of JavaBean

Example:
Insert a record in the past.
Establish a connection ..
Create operation object
Execute SQL ..

Write it now
Read ing Configuration
JavaBean value object. Set field name (field value ).
Save operation

The most basic benefit is that the ing configuration file can be directly changed when the relationship changes. You do not need to change the language names (mainly SQL statements) in the source file ).

In struts, action is used to access the business logic controller.
Http://www.inf.ufsc.br /~ Bosco/downloads/

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.