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

Spring-案例

时间:2017-08-20 10:13:44      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:接口   2.2.3   version   方法   inf   ext   getc   XML   xmlns   

1.导包

<!--beans-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<!--context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
2.创建类和接口

技术分享

2.1 颜色

2.1.1ColorInk 类中

public class ColorInk implements Ink {

    public String getColor() {
        return "红";
    }
}

2.1.2 GrayInk 类中

public class GrayInk implements Ink {

    public String getColor() {
        return "白";
    }
}

2.1.3 Ink 接口中

public interface Ink {
    //获取颜色的方法
    public String getColor();
}

 

2.2 纸张

2.2.1 A4Paper 类中

public class A4Paper implements Paper {
    public String getPaper() {
        return "A4纸";
    }
}

2.2.2 B5Paper 类中

public class B5Paper implements Paper {
    public String getPaper() {
        return "B5纸";
    }
}

2.2.3 Paper接口中

public interface Paper {
     public String getPaper();
}

2.3 Printer

技术分享
public class Printer {

    private Ink ink;

    private Paper paper;

    public void print(){
        System.out.println("用 "+ink.getColor()+" 颜色的墨盒在 "+paper.getPaper()+" 上打印出来 老原你敢下课吗???");
    }
    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;
    }
}
技术分享

3.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
      
    <!--IOC-->
    <bean id="HappyService" class="cn.bdqn.service.HappyService">
        <!--DI依赖注入-->
        <property name="info" value="Spring"></property>
    </bean>

    <!--打印机-->
    <bean id="colorInk" class="cn.bdqn.printer.ink.ColorInk"></bean>
    <bean id="grayInk" class="cn.bdqn.printer.ink.GrayInk"></bean>

    <bean id="b5Paper" class="cn.bdqn.printer.paper.B5Paper"></bean>
    <bean id="a4Paper" class="cn.bdqn.printer.paper.A4Paper"></bean>

    <bean id="pinter" class="cn.bdqn.printer.print.Printer">
        <property name="ink" ref="colorInk"></property>
        <property name="paper" ref="a4Paper"></property>
    </bean>
</beans>
技术分享

 

4.测试

@Test
//打印机
public void test02(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Printer pp= (Printer)ctx.getBean("pinter");
pp.print();
}

Spring-案例

标签:接口   2.2.3   version   方法   inf   ext   getc   XML   xmlns   

原文地址:http://www.cnblogs.com/s1297-lgy/p/7398794.html

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