Spring核心概念

初始Spring

1. 企业级应用开发

企业级应用是指那些为商业组织、大型企业而创建并部署的解决方案及应用。这些大型企业级应用的结构复杂,涉及的外部资源众多、事务密集、数据量大、用户数多,有较强的安全性考虑。

2. Spring的“绿草丛”

  • 轻量级框架,JavaEE的春天,当前主流框架
  • “站立式”的企业应用开发框架

目标:

  • 实现有的技术更加易用,推进编码最佳实践

Spring体系结构
Spring体系结构

Spring IoC

1. 理解“控制反转”

控制反转(Inversion of Control, IoC),也称为依赖注入(Dependency Injection, DI),是面向对象编程中的一种设计理念,用来减低程序代码之间的耦合度。
通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。

2. Hello Spring!

  1. 下载所需要的版本的Spring资源

Spring Framework3.2.13目录结构

  • docs:该文件夹下面包含Spring的相关文件,包含API参考文档、开发手册
  • libs:该文件夹下存放Spring各个模块的jar文件,每个模块提供三项内容:开发所需的jar文件、 以“javadoc”后缀表示的API和以“-sources”后缀表示的源文件
  • schema:配置Spring的某些功能时需要用到的schema文件,对于已经集成了Spring的IDE环境(如MyEclipse),这些文件不需要专门导入
  • HelloSpring需要的jar文件
    HelloSpring需要的jar文件

3. 依赖注入

如何开发一个打印机模拟程序,使其符合以下条件?

 可以灵活的配置使用彩色墨盒或灰色墨盒。
 可以灵活地配置打印页面的大小。

步骤
(1)定义Ink和Paper接口
(2)使用Ink接口和Paper接口开发Printer程序。在开发程序时并不依赖Ink和Paper具体的实现类
(3)开发Ink接口和Paper接口实现类:ColorInk、GreyInk和TextPaper
(4)组装打印机,运行调试

  • 定义Ink接口和Paper接口
/**
 * 墨盒接口
 */
public interface Ink {
	/**
	 * 定义打印采用的颜色的方法
	 * @param r 虹色
	 * @param g 绿色
	 * @param b 蓝色
	 * @return 返回打印采用的颜色
	 */
	public String getColor(int r, int g, int b);
}
/**
 * 纸张接口
 */
public interface Paper {
	public static final String NEWLINE = "\r\n";
	/**
	 * 输出一个字符到纸张
	 * @param c
	 */
	public void putInChar(char c);
	/**
	 * 得到输入纸张上的内容
	 * @return
	 */
	public String getContent();
}
  • 使用Ink接口和Paper接口开发Printer程序
/**
 * 打印机程序
 */
public class Printer {
	// 面向接口编程,而不是具体的实现类
	private Ink ink;
	// 面向接口编程,而不是具体的实现类
	private Paper paper;
	
	/**
	 * 设置注入所需的setter方法
	 * @param ink 传入墨盒参数
	 */
	public Ink getInk() {
		return ink;
	}
	public void setInk(Ink ink) {
		this.ink = ink;
	}
	/**
	 * 设置注入所需的setter方法
	 * @param paper 传入纸张参数
	 */
	public Paper getPaper() {
		return paper;
	}
	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	/**
	 * 打印机打印方法
	 */
	public void print(String str) {
		System.out.println("使用" + ink.getColor(255, 255, 0) + "颜色打印:\n");
		for(int i = 0; i<str.length(); i++) {
			paper.putInChar(str.charAt(i));
		}
		System.out.println(paper.getContent());
	}
  • 开发Ink接口和Paper接口实现类:ColorInk、GreyInk和TextPaper
/**
 * 灰色墨盒
 */
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);
	}
}
/**
 * 彩色墨盒
 */
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);
	}
}
/**
 * 文本打印纸张实现
 */
public class TextPaper implements Paper {
	private int charPerLine = 16;		//每行字符数
	private int linePerPage = 5;	//每页行数
	private String content = "";		//纸张中的内容
	private int posX = 0;		//当前横向位置,从0到charPerLine-1
	private int posY = 0;		//当前行数,从0到linePerPage-1
	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;
		}
	}
	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;
	}
}
  • 组装打印机,运行调试
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <bean id="ZG" class="pojo.Greeting">
    	<property name="person" value="张嘎" />
    	<property name="works" value="廖嘉鑫就是一头死猪" />
    </bean>
    <bean id="Rod" class="pojo.Greeting">
    	<property name="person" value="rod" />
    	<property name="works" value="世界上有10种人,认识二进制的和不认识二进制的" />
    </bean>
    <bean id="colorInk" class="dao.impl.ColorInk" />
    <bean id="geryInk" class="dao.impl.GreyInk" />
    <bean id="a4Paper" class="dao.impl.TextPaper">
    	<property name="charPerLine" value="10" />
    	<property name="linePerPage" value="8" />
    </bean>
    <bean id="b5Paper" class="dao.impl.TextPaper">
    	<property name="charPerLine" value="6" />
    	<property name="linePerPage" value="5" />
    </bean>
    <bean id="printer" class="pojo.Printer" >
    	<property name="ink" ref="colorInk" />
    	<property name="paper" ref="b5Paper" />
    </bean>
</beans>
public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Printer printer = (Printer) context.getBean("printer");
		String content = "几位轻量级容器的作者曾骄傲的对我说:这些容器非常有"+
									"用,因为它们实现了“控制反转”。这样的说辞让我深感迷惑:控"+
									"制反转是框架所共有的特征,如果仅仅因为使用了控制反转就认为"+
									"这些轻量级容器与众不同,就好像在说”我的轿车是与众不同的,“"+
									"因为它只要4个字。";
		printer.print(content);
	}

Spring AOP

1. 理解“面向切面编程”

面向侧面的程序设计(aspect-oriented programming,AOP),又译作面向方面的程序设计、观点导向编程、剖面导向程序设计)是计算机科学中的一个术语,指一种程序设计范型。
切面的概念源于对面向对象的程序设计的改进,但并不只限于此,它还可以用来改进传统的函数。

基本概念

  • 切面(Aspect): 一个模块化的横切逻辑(或称横切关注点),可能会横切多个对象
  • 连接点(Join Point): 程序执行中的某个具体的执行点
  • 增强处理(Advice): 切面在某个特定连接点上执行的代码逻辑
  • 切入点(Pointcut): 对连接点的特征进行描述,可以使用正则表达式.增强处理和一个切入点表达式相关联,并在与这个切入点匹配的某个连接点上运行
  • 目标对象(Target object): 被一个或多个切面增强的对象
  • AOP代理(AOP proxy): 由AOP框架所创建的对象,实现执行增强处理方法等功能
  • 织入(Weacing): 将增强处理连接到应用程序中的类型或对象上的过程

2. 使用Spring AOP 实现日志输出

步骤

  • 在项目中添加Spring AOP相关的jar文件
  • 编写前置增强和后置增强实现日志功能
  • 编写Spring配置文件,对业务方法进行增强处理
  • 编写代码,获取带有增强处理的业务对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值