Spring入门(一) 环境搭建、IOC/AOP快速上手

环境搭建

构建 Spring 开发环境需要用到 6 个 jar 包,包括 5 个 Spring 基础包以及 1 个第三方日志包。

Spring FrameWork 下载教程:Spring Framework 5.0.0下载

日志 jar 包下载地址: commons-logging

将下载的 Spring FrameWork 压缩包解压后,进入 libs 目录,可以看到如下文件:

Spring 框架每一个模块由 3 个 jar 包组成,例如 aop 模块由以下三个 jar 包组成:

  • spring-aop-5.2.0.RELEASE.jar:二进制文件(.class 文件)
  • spring-aop-5.2.0.RELEASE-sources.jar:源码
  • spring-aop-5.2.0.RELEASE-javadoc.jar:在线帮助文档

Spring 框架基础包有以下 5 个:

  • spring-aop-5.2.0.RELEASE.jar:AOP 的基础实现
  • spring-beans-5.2.0.RELEASE.jar:IOC 的基础实现,包含访问配置文件、创建和管理 bean 等
  • spring-context-5.2.0.RELEASE.jar:Spring 提供的在基础 IoC 功能上的扩展服务
  • spring-core-5.2.0.RELEASE.jar:Spring 框架的核心,其他模块都要依赖它
  • spring-expression-5.2.0.RELEASE.jar:Spring 表达式语言

新建 Java 工程,创建 lib 目录,将上面提到的 6 个 jar 包拷贝到 lib 目录下,然后将它们添加到类库。

在 src 目录下添加配置文件 applicationContext.xml,文件内容如下:

<?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">

</beans>

如果在 Eclipse 中安装 Spring 插件 spring tool suite,创建配置文件时,插件就能为我们自动生成上面的内容:

当然,你也可以直接使用 STS(Spring 官网提供的集成 Spring 插件的 Eclipse)。

上述步骤完成后,项目结构如下所示:

 

 

IOC 快速上手

现在我们有一个 Student 类,类信息如下:

public class Student {
	private String no;
	private String name;
	private int age;
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [no=" + no + ", name=" + name + ", age=" + age + "]";
	}
}

按照以往的方式,如果我们要使用 Student 对象,需要经过创建对象、为对象的属性赋值两个步骤:

private static void createStudentByCommon() {
	Student student = new Student();
	student.setNo("s-001");
	student.setName("zhangsan");
	student.setAge(21);
	System.out.println(student);
}

使用 IOC 的方式获取 Student 对象也需要两个步骤。

一、在 applicationContext.xml 中配置 bean

配置 bean 主要需要三个参数:

  1. id:bean 唯一标识符,保证 bean 在 IOC 容器唯一
  2. class:bean 类型,对应 Java 类(需要使用全类名
  3. property:bean 属性,对应 Java 类属性
<bean id="stu_2" class="org.hu.entity.Student">
	<property name="no" value="s-002"></property>
	<property name="name" value="lisi"></property>
	<property name="age" value="24"></property>
</bean>

二、从 IOC 容器中获取 bean

想从 IOC 容器中拿取东西,首先要有 IOC 容器,我们需要用配置文件初始化 IOC 容器,然后就可以根据配置文件中 bean 的 id 获取 bean。由于 bean 都是 Object 类型,将 bean 强制转换为相应的类型即可:

private static void createStudentByIOC() {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	Student student = (Student)context.getBean("stu_2");
	System.out.println(student);
}

两种方式运行结果:

以上两种获取对象的方式都经历了创建对象、为对象赋值两个步骤。不同之处在于,通过传统方式获取对象需要我们手动完成这两个步骤;而通过 IOC 方式获取对象,IOC 容器会负责完成这两个步骤,我们只需要从 IOC 容器中拿取对象就可以了。

 

AOP 快速上手

想要使用 AOP 功能,需要在项目中再引入 2 个第三方 jar 包:

将上面 2 个包添加到类库后,在 applicationContext.xml 配置文件中勾选 aop 支持:

现在我们有一个学生服务的接口,里面声明有添加学生的方法:

public interface IStudentService {
	void addStudent(Student stu);
}

实现该接口:

public class StudentServiceImpl implements IStudentService {
	@Override
	public void addStudent(Student stu) {
		System.out.println("add student:" + stu.getName() + "," + stu.getAge());
	}
}

我想大家在项目开发过程中,应该都写过类似下面的代码:

private static void LogBeforeByCommon() {
	IStudentService iStudentService = new StudentServiceImpl();
	Student stu = new Student();
	stu.setName("wangwu");
	stu.setAge(25);
	System.out.println("start to add student...");
	iStudentService.addStudent(stu);
}

打印 log 可以帮助我们了解相关代码是否执行,但是等到项目通过测试后,我们还需要把自己亲手写下的打印 log 代码注释掉或删除(说到这里,我就想起了曾经被分配到的一个任务:删除项目中 log 相关的代码,删的我想死的心都有了)。像打印 log 这样的代码,通常会散乱的分布在项目各个角落,它们和业务逻辑没有什么关联,但是又和业务代码牢牢的粘合在一起。为了让这类代码和业务代码解耦,AOP 出现了。

下面我们就实际体验一下 AOP。

首先,既然 AOP 的目的是解耦业务代码和非业务代码(打印 log),那么我们就要把非业务代码抽离出来。其次,为了让 Spring 区分哪些是非业务代码,就要遵循一定的规范。

结合以上两点,创建 LogBefore 类并实现 aop 包中提供的 MethodBeforeAdvice 接口,重写 before() 方法:

public class LogBefore implements MethodBeforeAdvice {
	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		System.out.println("start to add student...");
	}
}

现在业务代码和非业务代码都已编写完成,如何将二者联系起来呢?答案是配置文件。

在 applicationContext.xml 中添加 aop 相关配置:

<bean id="stuSerImp" class="org.hu.service.StudentServiceImpl"/>
<bean id="logBef" class="org.hu.aop.LogBefore" />
<aop:config>
    <aop:pointcut id="addstu"
       expression="execution(public void org.hu.service.StudentServiceImpl.addStudent(org.hu.entity.Student))" />
    <aop:advisor advice-ref="logBef" pointcut-ref="addstu"/>
</aop:config>

这里我主要解释一下两个标签的作用:

  1. aop:pointcut:切点。用于指定业务代码(某个类里的某个方法)。
  2. aop:advisor:通知。用于指定非业务代码(需要以 bean 形式配置)以及需要关联的业务代码。

一个需要注意的地方是 aop:pointcut 标签中的 expression 属性用于指定具体的方法,指定方法时需要将该方法和使用到参数类型的全名写明。

编写测试代码:

private static void LogBeforeByAOP() {
	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	IStudentService iStudentService = (IStudentService)context.getBean("stuSerImp");
	iStudentService.addStudent((Student)context.getBean("stu_2"));
}

还有一个需要注意的点:切点方法所在的类,需要以 bean 形式配置在 IOC 容器中,否则 AOP 无法生效。也就是说如果我们通过 new 的方式得到一个 StudentServiceImpl 对象,执行上面的代码并不会打印 “start to add student...”。

两种方式运行结果:

 

项目代码地址:https://github.com/BWHN/spring

 

参考:

Spring的各个jar包的作用介绍

Spring AOP 简介以及简单用法

自己实现Spring AOP(一)环境搭建及知识准备

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值