Spring Learning Notes: Section 2 Spring IOC Getting Started

xiaoxiao2021-03-05  23

Chapter 2 Spring IOC entry instance

Spring modularization is very strong, and each function module is independent, we can choose from. This chapter begins with Spring's IOC. The so-called IOC is a mode that uses XML to define a mode of generating an object, let's see if it is used.

1, data model.

1. There are three classes shown below, and Human (human) is an interface. The Chinese is a subclass, and American (Americans) is another subclass.

The source code is as follows:

Package cn.com.chengang.spring;

Public interface human {

Void Eat ();

Void Walk ();

}

Package cn.com.chengang.spring;

Public class chinese imports human {

/ * (Non-javadoc)

* @see cn.com.chengang.spring.human # Eat ()

* /

Public void Eat () {

System.out.println ("Chinese people have a set of food");

}

/ * (Non-javadoc)

* @see cn.com.chengang.spring.human # walk ()

* /

Public void walk () {

System.out.println ("Chinese people travel as flying");

}

}

Package cn.com.chengang.spring;

Public class American imports human {

/ * (Non-javadoc)

* @see cn.com.chengang.spring.human # Eat ()

* /

Public void Eat () {

System.out.println ("Americans are mainly bread-based");

}

/ * (Non-javadoc)

* @see cn.com.chengang.spring.human # walk ()

* /

Public void walk () {

System.out.println ("Americans are in a car, there is a tendency to degenerate in limbs);

}

}

2. The usage of the factory model is as follows

Create a factory class Factory as follows. Two string often defined in this factory class, identifying different people. The gethuman method determines what kind of human species to be generated based on the string of the incoming parameters.

Package cn.com.chengang.spring;

Public class factory {

Public final static string chinese = "Chinese";

Public final static string American = "american";

Public human gethuman (string ethnic) {

IF (Ethnic.equals (Chinese))

Return new Chinese ();

Else IF (Ethnic.Equals (American))

Return new American ();

Else

Throw new IllegalargumentException ("Parameter (Man) Error");

}

}

Below is a test program that uses factory methods to get different "human objects" and perform corresponding methods.

Package cn.com.chengang.spring;

Public class clienttest {public static void main (String [] args) {

Human human = null;

Human = new factory (). gethuman (factory.Chinese);

Human.eat ();

Human.walk ();

Human = new factory (). gethuman;

Human.eat ();

Human.walk ();

}

}

The print results of the console are as follows:

3. Usage of Spring's IOC is as follows:

1. Create a bean.xml file in the project root directory

The location of bean.xml is as follows, be careful not to look at it as a lib directory, it is in the myspring directory.

2. Modify the ClientTest program as follows:

Package cn.com.chengang.spring;

Import org.springframework.context.ApplicationContext;

Import org.springframework.context.support.filesystemxmlapplicationContext;

Public class clienttet {

Public final static string chinese = "Chinese";

Public final static string American = "american";

Public static void main (String [] args) {

// Human Human = NULL;

// human = new factory (). Gethuman (factory.Chinese);

// human.eat ();

// human.walk ();

// human = new factory (). Gethuman (factory.american);

// human.eat ();

// human.walk ();

ApplicationContext CTX = New FileSystemXMLApplicationContext ("bean.xml");

Human human = null;

Human = (human) CTX.getBean (Chinese);

Human.eat ();

Human.walk ();

Human = (human) CTX.getBean (American);

Human.eat ();

Human.walk ();

}

}

As can be seen from this program, CTX is equivalent to the original Factory factory, and the original factory can delete it. Then move the two constants in the Factory to the ClientTest class, the entire program structure is basically the same. Looking back at this sentence of the original bean.xml file

The ID is the parameter value of CTX.getBean, a string. Class is a class (package name class name). Then get the Chinese object in the ClientTest class is this

Human = (human) CTX.getBean (Chinese);

Because the getBean method returns the object type, you have to add a type conversion in front.

4, summary

(1) Perhaps some people say that IOC and factory model are not the same role, use IOC as much as possible.

For example, if the user needs changes, you must modify the Chinese class. Then, the previous plant mode is to change the method of the Factory class, and recompile the board. And IOC only needs to change the class attribute, and because IOC uses the Java reflex mechanism, these objects are dynamically generated. At this time, we can thermally plug in the Chinese object (do not have to stop the original procedure to rebuild)

(2) Perhaps someone says that it is so good, then I use all the objects all of the IOC.

Note that the flexibility of IOC has a price: set the steps to trouble, the way to generate objects is not intuitive, and the reflection is slower than the normal generated object. So use IOC to see if there is any need, I think that the more general judgment is: use IOC mode in places where factory models can be considered.

(3) In the way the IOC above, there are some places that can be varied. For example, bean.xml does not have to be placed in a project, or place elsewhere, such as cn.com.chengang.spring packages. However, it is also necessary to change when used, as shown below:

New FileSystemXmlapplicationContext ("SRC / CN / COM / CHENGANG / SPRING / Bean.xml");

In addition, bean.xml can also be changed to other names. This way we can set different bean.xml in the system.

(4) Low invasiveness on IOC.

What is low intrusion? If you use Struts or EJB, you will find that inherit some interfaces or classes can use their framework development. In this way, the system is bound to struts, EJB, and an adverse effect on the portability of the system. If the code is rarely related to a frame of code, then this framework can be called a low invasive framework.

Spring is very intrusive, and several classes such as HUMEN.JAVA, Chinese.java do not have to inherit what interfaces or classes. But there are still some spring shadows in ClientTest: FileSystemXmlapplicationContext class and CTX.getBean mode.

Now, low intrusion seems to be one of the standards that determine the technical quality of a framework.

(5) Usage about bean.xml

Bean.xml usage is still a lot, of which content is quite rich. Suppose there is a HUMENNAME attribute (name) in the Chinese class, then the original bean.xml is modified as follows. After you generate the Chinese object, "Chen Gang" will be automatically set to the HumenName property of the Chinese class. And because Singleton is True, generate the Chinese object will use a single case mode, and the system only exists instances.

Chen Gang

About Bean.xml other usage, no longer introduce, everyone will understand it with Spring documents.

转载请注明原文地址:https://www.9cbs.com/read-32132.html

New Post(0)