Spring:IoC和DI完成打印机打印详细说明过程及代码

Spring:IoC和DI完成打印机

在这里插入图片描述

课后作业
使用SpringIoCDI 装配一台打印机
纸张接口
实现 有: A4 A5
墨盒接口
实现 有:黑白 彩色
注解方式和非注解方式都要
说明:
    
    
1.首先是注解方式,下面那些代码就是用注解方式做的,注解方式就是在那个定义属性和方法的那个类上面添加注解:@Component("computer"),这个computer就是填写当前所在的类,然后在每一个属性前面加@Autowired,属性是是私有的,然后用类或接口来做对象,例如private Paper paper;然后当前类要写get/set方法和构造方法(有参无参都要),然后还要在写一个打印出内容的方法,可以自己随便命名,然后方法内要调用对象(paper)点方法名, 由多少个属性,就要写多少个对象点方法名,因为属性的类型是类名。然后方法的实现类,不是接口,如果有接口,接口里面写的是抽象方法,要在具体实现这个方法的类中的类名声明前面添加@Component,如果有多个类实现了这个接口,就只用在一个实现了中写@Component,不然会报错found 2.
2.测试类中ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");为固定写法,applicationContext可以自己起名字。Printer print=(Printer) applicationContext.getBean("printer");这个printer是那个创建属性和方法的类名。print.printCosole(); 调用打印的方法。 测试类基本比较固定,基本不用改什么。
3.applicationContext.xml,如果用注解就要看一下那个最前面那些文件,因为用注解的话是要添加一些东西的,不过都已经封装好了的,直接用就可以,而且封装好的这个xml也可以用于不注解的。

注解和不注解的区别:
注解的话就是要:指定存在需要被扫描的类对应包 -> 告诉Spring 容器你可以在哪个包中找到被Spring注解标,也就是写get/set和构造方法的那个类所在的包。这句话放到没有注解里面也没事,不会有什么影响。因为它只是扫描一下包名而已,对于不注解的可有可无,但是注解的一定要有
不注解的话:就是把上面那些@Component@Autowried全部去掉,一般来说都是先写无注解然后在写有注解的。无注解一共分三种。
		
	
  • 目录
    在这里插入图片描述

在这里插入图片描述

生成Referenced Libraries的操作方法
在这里插入图片描述

创建config的方法

在这里插入图片描述

代码:

package cn.yueqian.pojo;

import org.springframework.stereotype.Component;


public class A4Paper implements Paper {

	@Override
	public String getSize() {
		// TODO Auto-generated method stub
		return "A4";
	}

}
package cn.yueqian.pojo;

import org.springframework.stereotype.Component;

@Component
public class A5Paper implements Paper {

	@Override
	public String getSize() {
		// TODO Auto-generated method stub
		return "A5";
	}

}
package cn.yueqian.pojo;

import org.springframework.stereotype.Component;


public class ColorInk implements Ink{

	@Override
	public String getColor() {
		// TODO Auto-generated method stub
		return "彩色";
	}	

}
package cn.yueqian.pojo;

import org.springframework.stereotype.Component;

@Component
public class GrayInk implements Ink{

	@Override
	public String getColor() {
		// TODO Auto-generated method stub
		return "黑白";
	}
}

package cn.yueqian.pojo;

import org.springframework.stereotype.Component;

public interface Ink {
	public String getColor();


}

package cn.yueqian.pojo;

import org.springframework.stereotype.Component;

public interface Paper {
	public String getSize();

}

package cn.yueqian.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("printer")
public class Printer {
	@Autowired

	private Paper paper;
	
	@Autowired

	private Ink ink;
	

	public Printer() {
		super();
		// TODO Auto-generated constructor stub
	}


	public Printer(Paper paper, Ink ink) {
		super();
		this.paper = paper;
		this.ink = ink;
	}


	public Paper getPaper() {
		return paper;
	}


	public void setPaper(Paper paper) {
		this.paper = paper;
	}


	public Ink getInk() {
		return ink;
	}


	public void setInk(Ink ink) {
		this.ink = ink;
	}


	public void printCosole() {
		System.out.println("正在使用:"+ink.getColor()+"黑盒和:"+paper.getSize()+"纸张打印内容");
	}
	
	
}
	
package cn.yueqian.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.yueqian.pojo.Computer;
import cn.yueqian.pojo.Person;
import cn.yueqian.pojo.Printer;

public class Test {
	public static void main(String[] args) {
//     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//     Person p = (Person)context.getBean("person");
//     p.method();
//     
//     
//
//    Computer computer =(Computer) context.getBean("computer");
//    computer.run();
//    
//    Computer com1 =(Computer) context.getBean("conputer");
//    Computer com2 =(Computer) context.getBean("conputer");

		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Printer print=(Printer) applicationContext.getBean("printer");
		print.printCosole();     
	}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.1.xsd
"
>


<!-- 	<bean id="person" class="cn.yueqian.pojo.Person">
	<property name="name" value = "李四"></property>
	<property name="speaking" value = "有点饿"></property>
	</bean> -->
	
<!-- 	<bean id="host" class="cn.yueqian.pojo.Host"></bean> -->
<!-- <bean id="display" class="cn.yueqian.pojo.Display"></bean> -->

			
<!-- <bean id="computer" class="cn.yueqian.pojo.Computer"> -->
<!-- ref: 引用 容器中其他的bean实例 -->
<!-- DI:依赖注入 -->
<!-- <property name="host" ref="host"></property> -->
<!-- <property name="display" ref="display"></property> -->

<!-- <bean id="computer" class="cn.yueqian.pojo.Computer" scope = "prototype">  -->
<!-- <constructor-arg index = "0" ref = "host"></constructor-arg> -->
<!-- <constructor-arg index = "1" ref = "display"></constructor-arg> -->

<!-- <bean id = "computer" class = "cn.yueqian.pojo.Computer" autowire="byName"> -->

<!-- <bean id = "computer" class = "cn.yueqian.pojo.Computer" autowire="constructor"> -->
<!-- </bean> -->


<!-- <context:component-scan base-package="cn.yueqian.pojo"></context:component-scan>
 -->
    
    
    
    
    
<!--下面这个是打印的这个案例,上面是其他的案例的代码,也注释掉了,没关系,然后下面前三个就是无注解的用法,任选一个就可以,不然会冲突,最后一个有注解的--> 

<!--准备墨盒-->  <!--普通的写法,很麻烦,所以很少用这一种-->
		<!-- <bean id="grayink" class="cn.yueqian.pojo.GrayInk"></bean>
		<bean id="colorink" class="cn.yueqian.pojo.ColorInk"></bean>
		准备纸张
		<bean id="a4paper" class="cn.yueqian.pojo.A4Paper"></bean>
		<bean id="a5paper" class="cn.yueqian.pojo.A5Paper"></bean>
		打印机
		<bean id="printer" class="cn.yueqian.pojo.Printer">
			<property name="ink" ref="colorink"/>
			<property name="paper" ref="a4paper"/>
		</bean> -->	
            
            
<!--构造器写法,这个就要创建对应的构造器,里面的01代表第一个和第二个参数,然后class="cn.yueqian.pojo.Printer"后面有一个默认的参数,单实例对象singleton,这样就不会报错,如果设置为多实例的prototype,可能就会报错-->		
<!-- 		
		<bean id="printer" class="cn.yueqian.pojo.Printer">
<constructor-arg index="0" ref="ink"></constructor-arg>
<constructor-arg index="1" ref="paper"></constructor-arg>
</bean> -->

            
<!--自动装配(自动对bean实例做注入)-->            
<!-- <bean id="printer" class="cn.yueqian.pojo.Printer" autowire="constructor"> -->
<!-- </bean> -->
		
		
 <context:component-scan base-package="cn.yueqian.pojo"></context:component-scan>

</beans>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值