spring-Printer Case

Source: Internet
Author: User
Tags getcolor

1. Guide Package

<!--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. Creating classes and Interfaces

2.1 Colors

In the 2.1.1ColorInk class

 Public class Implements Ink {    public  String getColor () {        return ' Red ';    }}

2.1.2 in Grayink class

 Public class Implements Ink {    public  String getColor () {        return ' White ';    }}

2.1.3 The Ink interface

 Public Interface Ink {    // method to get color public      String getColor ();}

2.2 Paper

2.2.1 in A4paper class

 Public class Implements Paper {    public  String getpaper () {        return "A4 paper";    }}

2.2.2 in B5paper class

 Public class Implements Paper {    public  String getpaper () {        return "B5 paper";    }}

2.2.3 The paper interface

 Public Interface Paper {     public  String getpaper ();}

2.3 Printer

 Public classPrinter {Privateink Ink; PrivatePaper Paper;  Public voidprint () {System.out.println ("+ink.getcolor () +" color of the ink cartridges on the "+paper.getpaper () +" printed on the old original you dare to class??? "); }     PublicInk Getink () {returnInk; }     Public voidsetink (Ink ink) { This. Ink =Ink; }     PublicPaper Getpaper () {returnpaper; }     Public voidSetpaper (Paper Paper) { This. paper =paper; }}

3.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<!--ioc--> <bean id= "Happyservice"class= "Cn.bdqn.service.HappyService" > <!--di Dependency injection--<property name= "info" value= "Spring" ></prop Erty> </bean> <!--printer--<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> <propert Y name= "paper" ref= "A4paper" ></property> </bean></beans>

4. Testing

@Test
Printer
public void test02 () {
ApplicationContext ctx=new classpathxmlapplicationcontext ("Applicationcontext.xml");
Printer pp= (Printer) Ctx.getbean ("Pinter");
Pp.print ();
}

spring-Printer Case

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.