Feeling of learning spring IOC from cainiao and springioc

Source: Internet
Author: User

Feeling of learning spring IOC from cainiao and springioc
Zookeeper I. Introduction of spring IOC idea

In fact, it is really difficult for beginners to learn IOC, mainly because there is a loss of obstacle in understanding their ideas. But if they can overcome this obstacle, then you can quickly master the ideas. Literally speaking, IOC (Reverse Control) refers to a change in the control direction. We often encounter this sentence: "implementation must depend on abstraction, rather than abstract dependency implementation ." Although this sentence expresses the concept of Reverse control, it is really not a good understanding for beginners. Next we will use some examples to understand the meaning of the content.

First, we create two classes, one for connecting to the database, and the other for getting database data through connecting to the database.

(1) We usually write a data connection class, MysqlDatabaseConnection. java, where getData () is used to obtain data in the database.

Public class MysqlDatabaseConnection{……public list getData(){……}}

(2) when processing the business, we need to obtain data after connecting to the database. Therefore, we need to create a DoBussiness class when processing the business logic. The code is implemented as follows:

Public class  DoBussiness{Private MysqlDatabaseConnection db=new MysqlDatabaseConnection();……Public void getData(){……List list=db.getData();}}

In this way, our functions are basically implemented. However, we will find a problem through careful analysis, but when I want to change the database connection code, I find that the database connection code must be rewritten. For example, when we want to use Oracle to connect to the database, you have to write a class to connect to Oracle.

Public class OracleDatabaseConnection{……public list getData(){……}}

In this way, we will find that our business logic class DoBusiness depends on the database connection class. If we want to use MySQL today, we will change to Oracle tomorrow, and we will come to DB2 the day after tomorrow, what can we fucking do! If our company was a small company at the beginning, and as the company continues to grow and the business is constantly enriched and changing, it would be too CD to frequently modify our business logic class. Well, it seems that software refactoring is involved.

 

Here, we find that this is not a good design, because every business change involves a large number of program modifications. How can we design a pattern to solve this problem. To solve this problem, we need to understand that we need to implement a design model that can reuse the business logic. Let's think about it like this.

1. Compile a common interface class DatabaseConnection.

Public interface DatabaseConnection{……Public List getData();}

 
 


2. Write the classes responsible for database connection according to different services, for example, MysqlDatabaseConnection

Public class MysqlDatabaseConnection implements DatabaseConnection{……public LIst getData(){……}}

 
3. Write the business logic class DoBusiness, which only implements encoding for the interface, not for the specific implementation class (this is the implementation mentioned above must be dependent and abstract ).

Public class  DoBussiness{Private DatabaseConnection db=new DatabaseConnection();Public void setDatabaseConnection(DatabaseConnection db){this.db=db;}……Public void getData(){……List list=db.getData();}}

4. When we want to process the business, we can dynamically change it based on the database connection we want. At this time, we have taken measures to inject the database into the business logic-class DoBusiness (for your own good experience ).

Public class CallBussiness {Private DoBussiness do = new DoBussiness ();...... Public void getData (){...... // Inject the database. to modify the database, replace the injected database with Do. setDatabaseConnection (new MysqlDatabaseConnection (); List list = db. getData ();......}}

 

To sum up, let's look at how controls are reversed.

 

 

At first, the DoBusiness class was held by various database connections, that is, being controlled by specific database classes. However, when we implement database injection, we find that the situation has changed.

 
 

 

 

Due to the outstanding contribution of injection, the "dependency injection outstanding contribution" Award was awarded to the customer ". IOC is also called dependency injection DI.

2. Three implementation methods of dependency injection.

1. Interface injection: interface injection.

2. set injection: defines a Set method in the class to be injected, and defines the elements to be injected in the parameter.

3. constructor injection: Define a constructor in the class to be injected and define the elements to be injected in the parameters.

According to the injection method, there are different statements, which are not described here.

3. bean management.

The core container of Spring implements IOC, which aims to provide a non-intrusive framework. To achieve this, there are two basic and important packages in Spring, org. springframework. beans and org. springframework, context. A large number of Java reflection mechanisms exist in the code. Among the two packages, BeanFactory and ApplicationContext are the most important classes. ApplicationContext is built on BeanFactory, and some functions such as internationalization and event transfer are added.

1. Basic bean knowledge

(1) The bean flag is defined by Id or name.

(2) the class attribute of bean indicates the source of bean.

(3) bean deployment modes: shared and non-shared.

(4) Important bean property

(5) The bean dependency depends-on can force the initialization of multiple beans before the bean is initialized.

(6) The bean lifecycle can be defined by bean, bean initialization, and bean destruction.

 

2. bean Lifecycle

(1) bean definition is generally defined through the configuration document.

(2) bean initialization can be completed by using the init-method attribute.

Org. springframework. beans. factory. InitializationBean interface. If this interface is implemented, all necessary attributes are set by BeanFactory and the afterPropertiesSet () method is automatically executed.

(3) bean can be used in three ways: BeanWrapper, BeanFactory, ApplicationContext

(4) There are two methods for bean destruction: the first method is completed through the destroy-method attribute, and the second method is org. springframework. beans. factory. the destroy () method is automatically executed for the DisposableBean interface.

 

3. Automatic bean assembly

Bean autowire is used to specify automatic bean Assembly. There are 5 automatic assembly modes.

(1) byName mode: automatically assemble by bean attribute name. In Spring configuration Document XML, find a Bean with the same name as the property to be assembled.

(2) bytype mode: if there is a bean with the same property type in XML, this attribute will be automatically assembled.

(3) constructor mode: automatically assemble according to the parameters of the constructor.

(4) autodetect mode: Use the internal bean check class to select constructor or bytpe. Constructor and bytype.

(5) no Mode: automatic assembly is not used.

Although automatic assembly can reduce developers' input work, it is difficult to see whether every attribute of bean is set. Therefore, automatic assembly is not recommended. If Automatic Assembly is used, how does one check whether every attribute of bean is set? See Bean dependency check.

 

4. Bean dependency check

There are four dependency check modes: simple, object, all, none. Use the dependency-check attribute of bean to specify. Generally, dependency check and automatic assembly are used in combination.

(1) simple mode: Only checks the dependency of the basic type, string, and set.

(2) object Mode: Check the dependency of the dependent object

(3) all mode: Check the dependencies of all attributes.

(4) none mode: no dependency check is performed.

<If your understanding is incorrect, please do not hesitate to inform us>

Zookeeper
What is the role of spring IOC?

I understand IOC as interface-oriented programming.

For example, you need to use a disk to transmit data in the main method:
Disk a = new disk ();
But what if you want to use a USB flash drive to transmit data?
U disk u = new u disk ();?
This leads to excessive dependence on the underlying layer. The java idea is that the lower layer serves the upper layer rather than the lower layer.
Therefore, you must use the interface to enable both the disk and the USB flash disk to implement the method in the interface of the device. Interface I = new disk () or new U disk ();

Because java objects are created at runtime, rather than the compiler. But there is still a problem, that is, every time you want to modify the object type, you still need to modify it in the program. If you have an xml file, you can directly modify the xml file.

So this is the general idea of IOC.
Control inversion refers to transferring control of the underlying program to the interface !!! Implemented interface control!
Of course, this is only his core idea. There are other things in IOC that deserve further research.
Do you understand now?

How do I understand and use ioc in spring?

Ico is a file format, which is generally used for icons. Spring is similar to that of the buddy.

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.