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

SSM-Spring-02:Spring的DI初步加俩个实例

时间:2018-03-03 20:28:42      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:imp   schema   奔驰   图片   car   nts   printer   xmla   int()   

 

 

------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

 

 

DI:依赖注入

 

第一个DEMO:域属性注入

  java类:(Car类和Stu类,学生有一辆小汽车)

package cn.dawn.day02di;

/**
 * Created by Dawn on 2018/3/3.
 */
//小汽车类
public class Car {
    private String type;
    private String color;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}








package cn.dawn.day02di;

/**
 * Created by Dawn on 2018/3/3.
 */
//学生类
public class Stu {
    private String name;
    private Integer age;
    private Car car;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }
}

  配置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="car" class="cn.dawn.day02di.Car">
        <property name="type" value="奔驰"></property>
        <property name="color" value="红色"></property>
    </bean>
    <!--学生-->
    <!--这儿的小汽车不能用value,用ref引用上面的那个汽车car-->
    <bean id="stu" class="cn.dawn.day02di.Stu">
        <property name="name" value="孟六"></property>
        <property name="age" value="20"></property>
        <property name="car" ref="car"></property>
    </bean>

</beans>

  测试类

package cn.dawn.day02;

import cn.dawn.day02di.Stu;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180303 {
    @Test
    /*域属性*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day02.xml");
        Stu stu = (Stu) context.getBean("stu");
        System.out.println(stu.getName()+""+stu.getCar().getType());
    }
}

 

 

 

 

 

第二个Demo:打印机案例

  先把架构放上来

      技术分享图片

  把里面每个类中的代码(code) 放上来

package cn.dawn.day03printer.ink;

/**
 * Created by Dawn on 2018/3/3.
 */
/*墨盒*/
public interface Ink {
    public String getInkColor();
}


package cn.dawn.day03printer.ink;

/**
 * Created by Dawn on 2018/3/3.
 */
/*彩色墨盒*/
public class ColorInk implements Ink {
    public String getInkColor() {
        return "彩色墨盒";
    }
}



package cn.dawn.day03printer.ink;

/**
 * Created by Dawn on 2018/3/3.
 */
/*黑白墨盒*/
public class BlackInk implements Ink {
    public String getInkColor() {
        return "黑白墨盒";
    }
}



package cn.dawn.day03printer.paper;

/**
 * Created by Dawn on 2018/3/3.
 */
/*纸张*/
public interface Paper {
    public String getPagerSize();
}



package cn.dawn.day03printer.paper;

/**
 * Created by Dawn on 2018/3/3.
 */
/*B5纸张*/
public class B5Paper implements Paper{

    public String getPagerSize() {
        return "B5纸";
    }
}



package cn.dawn.day03printer.paper;

/**
 * Created by Dawn on 2018/3/3.
 */
/*A4纸张*/
public class A4Paper implements Paper {
    public String getPagerSize() {
        return "A4纸";
    }
}



package cn.dawn.day03printer.printer;

import cn.dawn.day03printer.ink.Ink;
import cn.dawn.day03printer.paper.Paper;

/**
 * Created by Dawn on 2018/3/3.
 */
/*打印机*/
public class Printer {
    /*墨盒*/
    private Ink ink;
    /*纸张*/
    private Paper paper;
    /*打印方法*/
    public void print(){
        System.out.println("我们的喷墨打印机,用"+ink.getInkColor()+""+paper.getPagerSize()+"打印出了------》我爱Spring");
    }


    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;
    }
}

 

 

 

  配置文件中:

 

<?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="ink" class="cn.dawn.day03printer.ink.ColorInk"></bean>
    <!--纸张-->
    <bean id="paper" class="cn.dawn.day03printer.paper.A4Paper"></bean>
    <!--打印机-->
    <bean id="printer" class="cn.dawn.day03printer.printer.Printer">
        <property name="ink" ref="ink"></property>
        <property name="paper" ref="paper"></property>
     </bean>
</beans>

 

 

 

  单测方法

 

package cn.dawn.day03;

import cn.dawn.day02di.Stu;
import cn.dawn.day03printer.printer.Printer;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180303 {
    @Test
    /*打印机案例*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day03.xml");
        Printer printer = (Printer) context.getBean("printer");
        printer.print();
    }
}

 

SSM-Spring-02:Spring的DI初步加俩个实例

标签:imp   schema   奔驰   图片   car   nts   printer   xmla   int()   

原文地址:https://www.cnblogs.com/DawnCHENXI/p/8502756.html

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