Spring concepts (Materials)

Source: Internet
Author: User
I. Spring IoC (inversion of control ).
This is a unique part of spring. IOC has been translated into "control inversion", and I don't know who translated the words so awkwardly. In fact, the principle is very simple. In plain words, the generated object is defined using XML. IOC is actually a design pattern. Spring only implements this design pattern.

How does this design model come from? Is gradually formed in practice.

Stage 1: Write Java in Normal ModeProgram. Generally, beginners must go through this stage.
Stage 2: The interface is frequently used. In this case, the interface is usually used in the factory mode.
Stage 3: Use the IOC mode. The factory mode is not good enough: (1) because of the generation of ClassesCodeWrite to the program. If you want to change a subclass, You need to modify the factory method. (2) An interface often means that a production factory will have many more factory classes.
The IOC mode can be seen as the sublimation of the factory mode, and IOC can be seen as a large factory, but the objects to be generated in this large factory are defined in the XML file, then, use Java's "reflection" programming to generate the corresponding object based on the class name given in XML. From the implementation point of view, IOC is to generate code for the previously written object in the factory method, which is defined by the XML file, that is, to separate the factory and object generation, the purpose is to improve flexibility and maintainability.

The most basic Java Technology in IOC is reflection programming. Reflection is a natural term. In general, reflection is used to generate an object based on the given class name (string. This programming method allows an object to decide which object to generate when it is generated. I also used reflection in a recent project. At that time, I provided one. in the properties text file, full class names (package name + class name) are written, and their objects are generated in the program according to these full class names. Reflection is widely used. For example, Hibernate and string use reflection as the most basic technical means.

In the past, the reflection programming method was 10 times slower than the normal object generation method, which may be why the reflection technology was not widely used at the time. However, after Sun's optimization, the reflection method generates the object and the normal object generation method, and the speed is not much different (but there is still a gap more than doubled ).

Therefore, to understand IOC, you must first understand the factory model and reflection programming. Otherwise, you cannot fully understand the cause and effect and implementation principles of IOC. As long as you understand this, you can also implement an IOC framework in your program. This is not only related to XML parsing and other knowledge, it is a little troublesome.

What are the biggest benefits of IOC? Because the object generation is defined in XML, when we need to change an implementation subclass, it will become very simple (generally, such an object is actually an interface ), you only need to modify the XML, so that we can even implement the Hot Plug-in of the object (a bit like the USB interface and scis hard disk ).

What are the biggest disadvantages of IOC? (1) The process of generating an object becomes complicated (in fact, the operation is quite simple). For those who are not used to this method, they will feel awkward and inintuitive. (2) object generation is inefficient because reflection programming is used. However, this loss is negligible compared with IOC's increased maintainability and flexibility, unless the generation of an object requires a particularly high efficiency. (3) There is a lack of support for IDE refactoring operations. If you want to rename the class in eclipse, you still need to manually change it in the XML file, this seems to be the shortcoming of all XML methods.

In general, the principle and implementation of IOC are quite simple. Some people once think that IOC has no practical effect. This is understandable because if you seldom use interfaces or use factory models in programming, therefore, you do not have a strong need to use IOC at all, and you will not understand the value of IOC. Some people also say they want to eliminate the factory and Singleton models, but they are all vague and clear. However, if you see the IOC mode and use spring, the factory mode and Singleton mode are basically useless. But does it disappear? No! Spring's IOC implementation is itself a big factory, which also includes the single-instance object generation method. As long as one setting is used, the object generation mode can be changed from the common mode to the single instance mode, it is very simple.

Summary:
(1) the IOC principle is very simple, and its role is also very targeted. Do not look at it very well.
(2) To understand IOC, first understand the concepts of "factory, interface, reflection.
II. Implementation of IOC in spring
I understand the idea and advantages of the IOC model, and then learn how to implement it. The above roughly describes the implementation of picocontainer and spring on IOC respectively. This article will take a detailed look at its implementation in spring.

In spring, IOC runs through its entire framework, but as martinflower says: "saying that these lightweight containers are special because they use inversion of control is like saying my car is special because it has wheels", which is already called an essential part of framework design. In terms of implementation, spring adopts a configuration file to implement dependency injection, and supports type2 IOC (setter injection) and type3 IOC (constructor injection ).

The core of IOC implementation in spring is its core Bean Factory, which assembles components in the framework with a certain degree of coupling, it also provides a service-oriented programming model (SOP: Service-orient programming) for its applications, such as AOP in spring and persistence (Hibernate and ibatics.
First, let's look at org. springframework. Beans. Factory. Bean

Factory interface, which is a very simple interface. The getbean method is the most important method. spring usually uses XML for populate bean, so xmlfactorybean is commonly used.

Use a simple example to check its usage. First, write down two bean classes:

Examplebean class:

Public class examplebean {

Private string psnname = NULL;

Private refbean = NULL;

Private string addinfo = NULL;

Public String getaddinfo (){

Return getrefbean (). getaddress () + getrefbean (). getzipcode ();

}

Public String getpsnname (){

Return psnname;

}

Public void setpsnname (string psnname ){

This. psnname = psnname;

}

Public void setrefbean (refbean ){

This. refbean = refbean;

}

Public refbean getrefbean (){

Return refbean;

}

Public void setaddinfo (string addinfo ){

This. addinfo = addinfo;

}

}

Refbean class:

Public class refbean {

Public String getaddress (){

Return address;

}

Public void setaddress (string address ){

This. Address = address;

}

Public String getzipcode (){

Return zipcode;

}

Public void setzipcode (string zipcode ){

This.zip code = zipcode;

}

Private string zipcode = NULL;

Private string address = NULL;

}

Its xml configuration file bean. xml

<? XML version = "1.0" encoding = "UTF-8"?>

<! Doctype beans public "-// spring // DTD bean // en"

Http://www.springframework.org/dtd/spring-beans.dtd>

<Beans>

<Bean id = "examplebean" class = "test. examplebean">

<Property name = "psnname"> <value> xkf </value> </property>

<Property name = "refbean">

<Ref bean = "refbean"/>

</Property>

</Bean>

<Bean id = "refbean" class = "test. refbean">

<Property name = "Address"> <value> Beijing </value> </property>

<Property name = "zipcode"> <value> 100085 </value> </property>

</Bean>

</Beans>

Then you can write a test class to test, of course, the need for spring Spring-core.jar and commons-logging.jar, of course, in Elipse can be easily achieved by installing the spring-ide plug-in.

Public class test {

Public static void main (string [] ARGs ){

Try {

Resource input = new classpathresource ("test/bean. xml ");

System. Out. println ("resource is:" + input );

Beanfactory factory = new xmlbeanfactory (input );

Examplebean EB =

(Examplebean) Factory. getbean ("examplebean ");

System. Out. println (EB. getpsnname ());

System. Out. println (EB. getaddinfo ());

}

Catch (exception e ){

E. printstacktrace ();

}

}

In this way, through beanfactory's getbean method and xml configuration file, the examplebean can be directly instantiated in the test class, eliminating the coupling between the application (test) and the Service (examplebean, IOC or dependency injection is implemented ).

">" Refbean ">

<Ref bean = "refbean"/>

</Property>

</Bean>

<Bean id = "refbean" class = "test. refbean">

<Property name = "Address"> <value> Beijing </value> </property>

<Property name = "zipcode"> <value> 100085 </value> </property>

</Bean>

</Beans>

Then you can write a test class to test, of course, the need for spring Spring-core.jar and commons-logging.jar, of course, in Elipse can be easily achieved by installing the spring-ide plug-in.

Public class test {

Public static void main (string [] ARGs ){

Try {

Resource input = new classpathresource ("test/bean. xml ");

System. Out. println ("resource is:" + input );

Beanfactory factory = new xmlbeanfactory (input );

Examplebean EB =

(Examplebean) Factory. getbean ("examplebean ");

System. Out. println (EB. getpsnname ());

System. Out. println (EB. getaddinfo ());

}

Catch (exception e ){

E. printstacktrace ();

}

}

In this way, through beanfactory's getbean method and xml configuration file, the examplebean can be directly instantiated in the test class, eliminating the coupling between the application (test) and the Service (examplebean, IOC or dependency injection is implemented ).

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.