Spring Ioc/di (Inversion control/Dependency injection) _ Getting Started Demo

Source: Internet
Author: User

Software 152 Liu Anmin

In the normal Java application development, we want to implement a certain function or to complete a business logic at least two or more objects to collaborate to complete, when not using spring, each object in need to use his partner object, they have to use like new object () Such a syntax to create a cooperative object, this cooperation object is created by their own initiative, the initiative to create a cooperative object in their own hands, which partners need to work on their own initiative to create, the initiative to create cooperation objects and the creation of the timing is self-control, and this will make the coupling between the object is high, A object needs to use partner B to do one thing together, a to use B, then A has a dependency on B, which is a coupling between A and B, and is tightly coupled, and the use of spring is not the same, the work of creating partner B is done by spring, Spring creates a B object and stores it in a container, and when the A object needs to use a B object, spring takes the B object that is in the container that holds the object, and then gives it to the A object, as to how spring creates that object, and when to create the object's , a object does not need to care about these details (when you were born, how it was born I do not care, can help me to work on the line), a get spring to our object, two people together to complete the work to complete.

So the control reversal IoC (inversion of control) is to say that the creation of the object is transferred, the initiative of the previously created object and the creation time is controlled by itself, and now this power is transferred to the third party , For example, the transfer to the IOC container, it is a factory dedicated to creating objects, you want what object, it gives you what object, with the IOC container, the dependency is changed, the original dependency is gone, they all rely on the IOC container, through the IOC container to establish their relationship.

This is my understanding of Spring's IOC( inversion of Control ) . Di( Dependency Injection ) is actually another argument of the IOC, which was first proposed by Martin Fowler in a paper in early 2004. He concludes: What control is reversed? is: the way to get dependent objects reverses the

* Three types of injection

(1). Setter (Common)

(2). Interface Injection

(3). Construction method

*demo Experiment: English letter case Conversion

A. Select the most commonly used injection type, create a new project, introduce the desired jar file, and then build the Applicationcontext.xml to configure the necessary data sources, with the following code:

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

<beans

Xmlns= "Http://www.springframework.org/schema/beans"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xmlns:p= "http://www.springframework.org/schema/p"

xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd ">

</beans>

B. Create an interface class: Changeletter, and then write a public return string change () method in this interface class, with the following code:

Public interface Changeletter {

Public String change ();

}

C. Create a new class lowwerletter with lowercase letters to uppercase and implement the Changeletter interface with the following code:

public class Lowwerletter implements Changeletter {

Private String str; Set string properties to implement setter methods, in order to facilitate injection in the configuration file

public void Setstr (String str) {

This.str = str;

}

@Override

Public String Change () {

Uppercase to lowercase

return Str.tolowercase ();

}

}

D. After completing the above steps, and then applicationcontext.xml the attribute injection, the code is as follows:

<bean id= "Changeletter" class= "Com.ansibee.imp.LowwerLetter" >

<property name= "str" >

<value>HIJKLMN</value>

</property>

</bean>

E. Now it is possible to test, create a new test class Testmain, the code is as follows:

public class Testmain {

public static void Main (string[] args) {

Changeletter Changeletter = (changeletter)

Applicationcontextutil.getapplicationcontext (). Getbean ("Changeletter");

System.out.println (Changeletter.change ());

}

}

After the test class is written, it can be tested, run Java application, observe the console, and find that the attribute value injected in the configuration file hijklmn into lowercase.

Spring Ioc/di (Inversion control/Dependency injection) _ Getting Started demo

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.