Spring's IOC and DI

Source: Internet
Author: User
Tags getcolor

First, what is spring?

Spring is a framework and the reason Java is not dead is because Java has the support of a very powerful technical framework of spring, and he is a lightweight Java development framework

Ii. What is the IOC?

  I. Meaning of the IOC: control reversal (full name inversion of controls)

He is an important object-oriented programming law to reduce the coupling of computer programs, but also the core of the lightweight spring framework

So how do you understand this sentence?

First understanding: Transfer control of a build object from the code itself to an external container

Second understanding: The ability to create object power is stripped from code control to IOC container control

 Second, the case

Introduction of Jar Package (lead node)

<!--spring-->        <dependency>            <groupId>org.springframework</groupId>            < artifactid>spring-beans</artifactid>            <version>4.2.0.RELEASE</version>        </ dependency>        <dependency>            <groupId>org.springframework</groupId>            < artifactid>spring-context</artifactid>            <version>4.2.0.RELEASE</version>        </ Dependency>        <!--AOP uses jar-->        <dependency>            <groupId> org.aspectj</groupid >            <artifactId> aspectjweaver</artifactid >            <version> 1.8.7</version >        </dependency>

There are a total of five jar packages (the first two nodes have two jar packages)

      

Create a class.

Package Demo01;import java.util.stringtokenizer;** * Create time:2018 March 05 16:28 * [email protected] Zhangxiao North * My motto:D o  Not, for one repulse, give up the purpose so resolved to effect **/public class Happyservice {    private String info;    Private Integer age;    Public Happyservice () {        System.out.println ("======happyservice");    }    public void work () {        System.out.println ("I Am" +info);    }    Public String GetInfo () {        return info;    }    Public Integer Getage () {        return age;    }    public void Setage (Integer age) {        this.age = age;    }    public void SetInfo (String info) {        this.info = info;    }}

Write Applicationcontext.xml file

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"       Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"       xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">    <bean id=" service "class=" Demo01. Happyservice ">        <property name=" info "value=" first spring Project "></property>        <property name=" Age "value=" ></property>    </bean></beans>

Test class:

Import demo01. Happyservice;import Demo03.printer.printer;import Demo03.printer.printer;import Org.junit.Test;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Create time:2018 March 05 16:35 * [Email Protected] Zhangxiao North * My motto:D o not, for one repulse, give up the purpose so you resolved to effect **/public class Test 20180303 {    @Test public    void T1 () {        applicationcontext context=new classpathxmlapplicationcontext (" Applicationcontext.xml ");        Happyservice service = (happyservice) context.getbean ("service");        Service.work ();    }}

Test results:

====haapyservice I was the first sring project

Iii. What is di

Dependency injection Dependency Injection to object injection property value

The difference between IOC and DI:

IOC control inversion: What is said is that the control of creating an object instance is stripped from the code control to the IOC container control, which is actually what you control in the XML file, focusing on the principle.

Di Dependency Injection: When an object instance is created, an attribute value or other object instance is injected for that object, focusing on implementation. They are a description of different aspects of spring's core thinking.

Case printer

1. Write The ink Cartridge interface

The interface of the package cn.spring.day03printer.ink;//Mill box is public interface Ink {public    String getColor ();}

2. Two kinds of cartridges

  

Package cn.spring.day03printer.ink;/** * Create time:2018 March 05 20:03 * [email protected] Zhangxiao North * My motto:D o not, for O Ne Repulse, give up the purpose so resolved to effect **/public class Blackink implements ink {    //monochrome cartridge            Publ IC String GetColor () {        return "blackikn";    }}

    

Package cn.spring.day03printer.ink;/** * Create time:2018 March 05 20:07 * [email protected] Zhangxiao North * My motto:D o not, for O Ne Repulse, give up the purpose so resolved to effect **/    //Color Cartridge public class Colorink implements ink {    Publ IC String GetColor () {        return "colorikn";    }}

3. The paper interface and his implementation class

Package cn.spring.day03printer.paper;//Paper Public interface paper {public    String getpaprsize ();}

  

Package cn.spring.day03printer.paper;/** * Create time:2018 March 05 20:13 * [email protected] Zhangxiao North * My motto:D o not, for One repulse, give up the purpose so resolved to effect **///A4 paper public class A4aper implements Paper {public    S Tring Getpaprsize () {        return "A4paper";    }}

  

Package cn.spring.day03printer.paper;/** * Create time:2018 March 05 20:15 * [email protected] Zhangxiao North * My motto:D o not, for  One repulse, give up the purpose so resolved to effect **///b5 of paper public class B5paper implements Paper {public    String getpaprsize () {        return ' b5paper ';    }}

4. Writing the printer class

Package Cn.spring.day03printer.printer;import Cn.spring.day03printer.ink.ink;import cn.spring.day03printer.paper.paper;/** * Create time:2018 March 05 20:18 * [email protected] Zhangxiao North * My motto:D o not, for O Ne Repulse, give up the purpose so you resolved to effect **///printer public class Printer {    private ink ink;    Private Paper Paper;    public void print () {        System.out.println ("Use" +paper.getpaprsize () + "\ T" +ink.getcolor () + "ink cartridge, print");    }    Public Ink Getink () {        return Ink;    }    public void Setink (Ink ink) {        This.ink = ink;    }    Public Paper Getpaper () {        return Paper;    }    public void Setpaper (Paper Paper) {        this.paper = Paper;    }}

5.XML file Configuration

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"       Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"       xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "><!--Customizing a ink-->    in spring <bean id= "Ink" class= "Cn.spring.day03printer.ink.ColorInk" >    </bean>    <bean id= "paper" class= "Cn.spring.day03printer.paper.A4aper" >    </bean>    <bean id= "printer" class= " Cn.spring.day03printer.printer.Printer ">        <property name=" paper "ref=" paper "></property>        <property name= "Ink" ref= "ink" ></property>    </bean></beans>

6. Test class

Import Cn.spring.day03printer.printer.printer;import Org.junit.test;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Create time:2018 March 05 20:29 * [Email Protected] Zhangxiao North * My motto:D o not, for one repulse, give up the purpose so you resolved to effect **/public class Text @Test public    void T1 () {        applicationcontext contest=new classpathxmlapplicationcontext (" Applicationcontext-day03printer.xml ");        Printer printer= (Printer) Contest.getbean ("Printer");        Printer.print ();    }}

Operation Result:

March 07, 2018 20:32:25 pm Org.springframework.context.support.ClassPathXmlApplicationContext Preparerefresh Info: refreshing org[email protected]7c883540:startup Date [Wed Mar 19:51:25 CST 2018]; Root of the context hierarchy March 07, 2018 20:32:25 pm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader Loadbeandefinitions info: Loading XML Bean Definitions from class path resource [Applicationcontext-day03printer.xml]  Print Process finished with exit code 0 using the A4PAPERCOLORIKN cartridge

  

Spring's IOC and DI

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.