上机练习1-在控制台输出
>使用Spring实现依赖注入
需求说明
>输出:
张嘎说:“三天不打小鬼子,手都痒痒!”
Rod 说:“世界上有10种人,认识二进制的和不认识二进制的。”
>说话人和说话内容都通过Spring注入。
实现思路及关键代码
(1)将 Spring 添加到项目
(2)编写程序代码和配置文件(同时配置张嘎和 Rod 两个 Bean)。
(3)获取 Bean 实例,调用功能方法。
1、打招呼的类
2、spring-config.xml
<bean id="zhangGa" class="com.Greeting"> <property name="person"> <value>张嘎</value> </property> <property name="words"> <value>三天不打小鬼子,手都痒痒!</value> </property> </bean>
<bean id="zhangGa" class="com.Greeting">
- id :表示 命名 bean实例的名称 【自定义】
- class: 表示 bean实例的类型 【类】
<property name="person">
<value>张嘎</value>
</property>
- name :类(上述,class的 那个类 )的 某个属性
- value: 该属性,赋值
——调用 该类 中 set方法 进行赋值
<?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="zhangGa" class="com.Greeting">
<property name="person">
<value>张嘎</value>
</property>
<property name="words">
<value>三天不打小鬼子,手都痒痒!</value>
</property>
</bean>
<bean id="rod" class="com.Greeting">
<property name="person">
<value>Rod</value>
</property>
<property name="words">
<value>世界上有10种人,认识二进制的和不认识二进制的。</value>
</property>
</bean>
</beans>
3、测试类
1. 通过 ClassPathXmlApplicationContext 实例化 spring 的上下文
——备注
2. 通过 ApplicationContext 的 getBean 方法,根据 id 获得 bean实例
——备注
3. 执行方法
—— 结果
所以,测试类 【代码如下】——————————————————
import com.Greeting;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("spring-config.xml");
Greeting zhangga=(Greeting)context.getBean("zhangGa");
Greeting rod=(Greeting)context.getBean("rod");
zhangga.sayGreeting();
rod.sayGreeting();
}
}
结果:
上机练习2-模仿实现打印机功能
需求说明
模仿示例7至示例 16 的内容,自己动手实现打印机功能,并使用Spring loC 实现墨盒和纸张的灵活替换。
1、接口类
- 墨盒
- 纸张
package com;
public interface Ink {
public String getColor(int r,int g,int b);
}
package com;
public interface Paper {
public static final String newline="\r\n";
public void putInChar(char c);
public String getContent();
}
2、(接口 开发)打印机
- 打印机
package com;
public class Printer {
private Ink ink=null;
private Paper paper=null;
public void setInk(Ink ink) {
this.ink = ink;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
public void print(String str){
System.out.println("使用"+ink.getColor(255,200,0)+"颜色打印:\n");
for (int i=0;i<str.length();++i){
paper.putInChar(str.charAt(i));
}
System.out.println(paper.getContent());
}
}
3、(接口)实现类
- 彩墨
- 灰墨
- 纸张
package com;
import java.awt.*;
public class ColorInk implements Ink {
@Override
public String getColor(int r, int g, int b) {
Color color=new Color(r,g,b);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
package com;
import java.awt.*;
public class GreyInk implements Ink {
@Override
public String getColor(int r, int g, int b) {
int c=(r+g+b)/3;
Color color=new Color(c,c,c);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
package com;
public class TextPaper implements Paper {
private int charPerLine=16;
private int linePerPage=5;
private String content="";
private int posX=0;
private int posY=0;
private int posP=1;
@Override
public void putInChar(char c) {
content+=c;
++posX;
if (posX==charPerLine){
content+=Paper.newline;
posX=0;
++posY;
}
if (posY==linePerPage){
content+="==第"+posP+"页==";
content+=Paper.newline+Paper.newline;
posY=0;
++posP;
}
}
@Override
public String getContent() {
String ret=this.content;
if (!(posX==0&&posY==0)){
int count=linePerPage-posY;
for (int i=0;i<count;++i){
ret+=Paper.newline;
}
ret+="==第"+posP+"页==";
}
return ret;
}
public void setCharPerLine(int charPerLine) {
this.charPerLine = charPerLine;
}
public void setLinePerPage(int linePerPage) {
this.linePerPage = linePerPage;
}
}
4、组装打印机(spring.xml)
- 彩墨
- 灰墨
- a4纸张
- b5纸张
- 打印机
<?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="colorInk" class="com.ColorInk"></bean>
<bean id="greyInk" class="com.GreyInk"></bean>
<bean id="a4Paper" class="com.TextPaper">
<property name="charPerLine" value="10"/>
<property name="linePerPage" value="8"/>
</bean>
<bean id="b5Paper" class="com.TextPaper">
<property name="charPerLine" value="6"/>
<property name="linePerPage" value="5"/>
</bean>
<bean id="printer" class="com.Printer">
<property name="ink" ref="colorInk"/>
<property name="paper" ref="b5Paper"/>
</bean>
</beans>
<bean id="printer" class="com.Printer"> <property name="ink" ref="colorInk"/> # 替换墨盒 <property name="paper" ref="b5Paper"/> # 替换纸张 </bean>
5、测试类
import com.Printer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Printer printer =(Printer) context.getBean("printer");
String content="几位请流量计急急急就一二三四五六七:一二三四五六七"+
"哦,一二三四五六七”一二三四“。这样的说辞让我深感魔火:空"+
"加哦加哦及给哦评价哦反应釜滚动条的";
printer.print(content);
}
}
总结
。。。。