spring之AOP的简单实例

AOP:面向切面编程,就是把除去业务部分以外的东西单独模块化,比如打日志等,就像学生信息的增删改查,可以把输出日志单独模块化出来,通过切面对的方式进行编程。

在进行实例编写之前先进行一些专业术语的了解

切面aspect:是对象操作过程中的截面,是一段代码,实现额外的模块化功能

连接点join point:是指对象操作过程中的某个阶段的点,连接点实际上是对象的一个操作

切入点pointcut:切入点是连接点的集合,切面与程序的交叉点就是程序的切入点。也就是切面注入到程序中的位置。换句话说,切面就是通过切入点被注入的。

通知advice:通知是摸个切入点被横切后,所采取的处理逻辑。在切入点处拦截程序后,通过通知来执行切面。

目标对象target:所有被通知的对象(也可以理解为被代理的对象)都是目标对象。目标对象被AOP所关注,随时准备向目标对象注入切面

织入Weaving:织入是将切面功能应用到目标对象的过程。由代理工厂创建一个代理对象,这个代理对象可以为目标对象执行切面功能,就是这个代理对象是根据目标对象织入切面后生成的。

实例的实现如下所示:

1.导入需要的jar包-这些是基本的包(可以看到整体结构)

 

 

2.编写实例类,Student类和需要增加额外功能的打印文件StudentPrint类代码如下所示:

 1 package com.gp.service;
 2 
 3 public class Student {
 4    public void addStudent(){
 5        System.out.println("------添加学生信息-------");
 6    }
 7    public void updateStudent(){
 8        System.out.println("------修改学生信息-------");
 9    }
10    public void deleteStudent(){
11        System.out.println("------删除学生信息-------");
12    }
13    public void selectStudent(){
14        System.out.println("------查询学生信息-------");
15    }
16 }

 

 1 package com.gp.service;
 2 
 3 public class StudentPrint {
 4    public void before(){
 5        System.out.println("执行学生功能前的准备工作----开始执行");
 6    }
 7    public void after(){
 8        System.out.println("学生功能实现后的打印工作----功能已实现,此处进行打印工作");
 9    }
10 }

 

3.实体类完成后,下面进行配置文件的编写:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:aop="http://www.springframework.org/schema/aop"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6            http://www.springframework.org/schema/beans/spring-beans.xsd
 7            http://www.springframework.org/schema/aop
 8            http://www.springframework.org/schema/aop/spring-aop.xsd" >
 9 
10   
11    <bean id="student" class="com.gp.service.Student"/>
12    <bean id="myStudent" class="com.gp.service.StudentPrint"/>
13    <aop:config>
14       <aop:pointcut expression="execution(* com.gp.service.Student.*(..))" id="pointcut1"/>
15       <aop:aspect ref="myStudent">
16          <aop:before method="before" pointcut-ref="pointcut1" />
17          <aop:after-returning method="after" pointcut-ref="pointcut1"/>
18       </aop:aspect>
19    </aop:config>
20   
21 </beans>

 

其中的部分说明:表达式中表示拦截Student类中的所有方法,唯一id为pointcut1,作为切入点,用作后面切面的切入

4.测试类TestStudent类的文件:

 

 1 package com.gp.test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.gp.service.Student;
 7 
 8 public class TestStudent {
 9   public static void main(String[] args) {
10      ApplicationContext ct=new ClassPathXmlApplicationContext("applicationContext.xml");
11      Student student=(Student)ct.getBean("student");
12      student.addStudent();
13      student.deleteStudent();
14      student.updateStudent();
15      student.selectStudent();
16   }
17 }

 

5.可以进行测试运行了,结果如下所示:

若是把配置文件中的表达式修改为:

<aop:pointcut expression="execution(* com.gp.service.Student.addStudent(..))" id="pointcut1"/>

则运行的结果为:

 

会根据匹配来决定对那些方法进行织入操作。

 

在运行的过程中遇到一些的问题,

1)JDK的版本和spring的版本问题,本人使用的是JDK1.6的,而spring使用5.0后的时,测试文件编译不通过,

ApplicationContext ct=new ClassPathXmlApplicationContext("applicationContext.xml");

这行会提示报错

2)jar包导入的不全时,aopalliance.jar包没有在下载的spring的jar包中,没有导入,结果运行时报如下错误:

java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice,导入aopalliance.jar的jar包还不能解决,需导入一下两个包:

aopalliance.jar ,aspectjweaver-1.7.4.jar即可

到此,AOP的简单实例就完成了,下面会附上相应的jar包的链接

https://pan.baidu.com/s/1b5IGRj4syjZoKFSjL7aRrw

 

posted on 2018-03-14 22:03  没有太晚的开始  阅读(384)  评论(0编辑  收藏  举报

导航