目录
1.创建一个maven项目,在pop.xml把需要的包导入进去
2.配置log4j.properties文件控制日志输出(也可以不用配,貌似没用到)
4.main下面创建resources目录,创建applicationContext.xml作为spring的配置文件,用元素定义Bean(组件)的实例
1.创建一个maven项目,在pop.xml把需要的包导入进去
4.编写接口的实现类:ColorInk、GreyInk、TextPaper
一、Spring IoC
本节将学会IoC(控制反转)-----也称依赖注入,是面向对象编程中的一种设计理念,用来降低程序代码之间的耦合度。
本节使用的是设值注入方法,之后会学习更多的注入方法
(本案例主要体现在applicationContext.xml里)
二、入门案例:Hello,Spring!
要求:
1.编写HelloSpring类输出“Hello,Spring!”
2.字符串“Spring”通过spring框架赋值到HelloSpring类中
实现整体目录结构:
1.创建一个maven项目,在pop.xml把需要的包导入进去
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
2.配置log4j.properties文件控制日志输出(也可以不用配,貌似没用到)
log4j.rootLogger = info,con
log4j.appender.con = org.apache.log4j.ConsoleAppender
log4j.appender.con.layout = org.apache.log4j.PatternLayout
log4j.appender.con.layout.ConversionPattern = %d{MM-dd HH:mm:ss}[%p]%c%n-%m%n
3.HelloSpring类
package com.chq.springdemo;
/**
* Hello,Spring!
*/
public class HelloSpring {
private String who = null;
public void print(){
System.out.println( "Hello" + this.getWho()+"!");
}
public String getWho(){
return who;
}
public void setWho(String who){
this.who = who;
}
}
4.main下面创建resources目录,创建applicationContext.xml作为spring的配置文件,用<bean>元素定义Bean(组件)的实例
<?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="helloSpring" class="com.chq.springdemo.HelloSpring">
<!--设值注入:调用setWho()方法实现赋值操作-->
<property name="who">
<!--将spring赋值给who属性-->
<value>spring</value>
</property>
</bean>
</beans>
5.测试
新建HelloSpringTest类
package com.chq.springdemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Unit test for simple App.
*/
public class HelloSpringTest {
/**
* Rigorous Test :-)
*/
public static void main(String[] args) {
//通过ClassPathXmlApplicationContext实例化Spring上下文
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过ApplicationContext的getBean()方法,根据id获取Bean的实例
HelloSpring helloSpring = (HelloSpring) context.getBean("helloSpring");
//执行pringt()方法
helloSpring.print();
}
}
结果:
三、案例:打印机
要求:
1.可以灵活配置使用彩色或灰色墨盒
2.可以灵活配置打印页面的大小(A4,B5等)
实现整体目录结构:
1.创建一个maven项目,在pop.xml把需要的包导入进去
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
2.定义Ink和Paper接口
package cn.ink;
/**
* 墨盒接口
*/
public interface Ink {
/**
* @param r 红色
* @param g 绿色
* @param b 蓝色
* @return 返回打印采用的颜色,如#ffc800
*/
public String getColor(int r,int g,int b);
}
package cn.paper;
/**
* 纸张接口
*/
public interface Paper {
public static final String newline = "\r\n";
/**
* 输出一个字符到纸张上
*/
public void putInChar(char c);
/**
* 得到输出到纸张上的内容
*/
public String getContext();
}
3.使用Ink和Paper接口开发Printer程序
package cn.printer;
import cn.ink.Ink;
import cn.paper.Paper;
/**
* 打印机程序
*/
public class Printer {
//面向接口编程,而不是具体的实现类
private Ink ink = null;
private Paper paper = null;
/**
* 设置注入所需的setter方法
* @param ink 传入墨盒参数
*/
public void setInk(Ink ink) {
this.ink = ink;
}
/**
* 设置注入所需的setter方法
* @param paper 传入纸张参数
*/
public void setPaper(Paper paper) {
this.paper = paper;
}
/**
* 打印机打印方法
* @param str 传入打印内容
*/
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.getContext());
}
}
4.编写接口的实现类:ColorInk、GreyInk、TextPaper
package cn.ink;
import java.awt.*;
/**
* 彩色墨盒
*/
public class ColorInk implements Ink {
public String getColor(int r, int g, int b) {
Color color = new Color(r,g,b);
return "#"+Integer.toHexString(color.getRGB()).substring(2);
}
}
package cn.ink;
import cn.ink.Ink;
import java.awt.*;
/**
* 灰色墨盒
*/
public class GreyInk implements Ink {
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 cn.paper;
import cn.paper.Paper;
/**
* 文本打印纸张实现
*/
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;
public String getContext() {
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 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;
}
}
public void setCharPerLine(int charPerLine) {
this.charPerLine = charPerLine;
}
public void setLinePerPage(int linePerPage) {
this.linePerPage = linePerPage;
}
}
5.组装打印机
main下面创建resources目录,创建applicationContext.xml作为spring的配置文件,用<bean>元素定义Bean(组件)的实例
<?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="cn.ink.ColorInk"/>
<bean id="greyInk" class="cn.ink.GreyInk"/>
<bean id="a4Paper" class="cn.paper.TextPaper">
<property name="charPerLine" value="10"/>
<property name="linePerPage" value="8"/>
</bean>
<bean id="b5Paper" class="cn.paper.TextPaper">
<property name="charPerLine" value="6"/>
<property name="linePerPage" value="5"/>
</bean>
<bean id="printer" class="cn.printer.Printer">
<property name="ink" ref="colorInk"/>
<property name="paper" ref="b5Paper"/>
</bean>
</beans>
6.测试打印机
package cn;
import cn.printer.Printer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PrinterTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Printer printer = (Printer) context.getBean("printer");
String content = "随便敲键盘测试一下这个程序,北京温度真低,我已经穿上了以往冬天的衣服,再冷一点就该买羽绒服啦!";
printer.print(content);
}
}
结果: