码迷,mamicode.com
首页 > 编程语言 > 详细

Spring之IOC和DI

时间:2018-03-07 21:37:48      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:依赖   tar   contest   ima   同方   实现   AC   info   code   

一、Spring是什么?

  Spring是一个框架,Java不死的原因是因为Java有Spring这个非常强大的技术框架的支持,而且他是一个轻量级的Java开发框架

二、什么是IOC?

  一、IOC的意思:控制反转(全称Inversion of Control)

    他是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,也是轻量级的Spring框架的核心

    那么如何理解这句话呢?

    第一种理解:将组建对象的控制权从代码本身转移到外部容器

    第二种理解:将创建对象实力的控制权从代码控制剥离到IOC容器控制

  二、案例

      引入jar包(引节点)

<!--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使用的jar-->
        <dependency>
            <groupId> org.aspectj</groupId >
            <artifactId> aspectjweaver</artifactId >
            <version> 1.8.7</version >
        </dependency>

    里面一共有五个jar包(前两个节点有两个jar包)

      技术分享图片

    创建一个类。

package demo01;

import java.util.StringTokenizer;

**
 * Create Time: 2018年03月05日 16:28
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
public class HappyService {
    private String info;
    private Integer age;

    public HappyService(){
        System.out.println("======HappyService");
    }

    public void work(){
        System.out.println("我是"+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;
    }
}

  写ApplicationContext.xml文件

<?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="第一个Spring项目"></property>
        <property name="age" value="10"></property>
    </bean>
</beans>

  测试类:

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年03月05日 16:35
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
public class Test20180303 {
    @Test
    public void t1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        HappyService service =(HappyService) context.getBean("service");
        service.work();
    }


}

  测试结果:

====HaapyService
我是第一个Sring项目

三、什么是DI

  Dependency Injection  依赖 注入   给对象注入属性值

  IOC和DI的区别:

    IOC控制反转:说的是创建对象实例的控制权从代码控制剥离到IOC容器控制,实际就是你在xml文件控制,侧重于原理。

    DI依赖注入:说的是创建对象实例时,为这个对象注入属性值或其它对象实例,侧重于实现。它们是spring核心思想的不同方面的描述。

  案例 打印机

    1.编写墨盒接口

package cn.spring.day03printer.ink;
//磨盒的接口
public interface Ink {
    public String getColor();
}

    2.两个种墨盒

  

package cn.spring.day03printer.ink;

/**
 * Create Time: 2018年03月05日 20:03
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
public class BlackInk implements Ink {
    //黑白墨盒        
    public String getColor() {
        return "BlackIkn";
    }
}

    

package cn.spring.day03printer.ink;

/**
 * Create Time: 2018年03月05日 20:07
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
    //彩色墨盒
public class ColorInk implements Ink {
    public String getColor() {
        return "ColorIkn";
    }
}

    3.纸张接口和他的实现类

package cn.spring.day03printer.paper;
//纸张
public interface Paper {
    public String getPaprSize();
}

  

package cn.spring.day03printer.paper;
/**
 * Create Time: 2018年03月05日 20:13
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
//A4纸
public class A4aper implements Paper {
    public String getPaprSize() {
        return "A4paper";
    }
}

  

package cn.spring.day03printer.paper;
/**
 * Create Time: 2018年03月05日 20:15
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
//B5的纸
public class B5Paper implements Paper {
    public String getPaprSize() {
        return "B5Paper";
    }
}

  4.编写打印机类

package cn.spring.day03printer.printer;
import cn.spring.day03printer.ink.Ink;
import cn.spring.day03printer.paper.Paper;
/**
 * Create Time: 2018年03月05日 20:18
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
//打印机
public class Printer {
    private Ink ink;
    private Paper paper;
    public void print(){
        System.out.println("使用"+paper.getPaprSize()+"\t"+ink.getColor()+"墨盒,打印");
    }
    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文件配置

<?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">
<!--在Spring中定制一个Ink-->
    <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.测试类

import cn.spring.day03printer.printer.Printer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Create Time: 2018年03月05日 20:29
 * C@author 张晓北
 * My motto :Do not , for one repulse , give up the purpose that you resolved to effect
 **/
public class Text03 {
    @Test
    public void t1(){
        ApplicationContext contest=new ClassPathXmlApplicationContext("applicationContext-day03printer.xml");
        Printer printer=(Printer)contest.getBean("printer");
        printer.print();
    }
}

  运行结果:

三月 07, 2018 20:32:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c883540: startup date [Wed Mar 07 19:51:25 CST 2018]; root of context hierarchy
三月 07, 2018 20:32:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext-day03printer.xml]
使用A4paper	ColorIkn墨盒,打印

Process finished with exit code 0

  

 

Spring之IOC和DI

标签:依赖   tar   contest   ima   同方   实现   AC   info   code   

原文地址:https://www.cnblogs.com/zhangyi0331/p/8516279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!