https://www.5ceo.cn/wyl/blogs/detail/1000156.html
AOP 面向切面编程。
降低模块之间的耦合度,提高开发效率,提高可维护性,可扩展性,可复用性
Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征。AOP包括切面、连接点、通知(advice)、切入点(pointCut) 。
1.aop几个概念:
横切关注点: 对哪些方面进行拦截,拦截后怎么处理。
切面(aspect):切面是横切关注点的抽象。
连接点(joinpoint):被拦截的方法
切入点(pointcut):对连接点进行拦截的定义。
通知(advice):拦截到连接点之后要执行的代码
目标对象:代理的目标对象
织入
引入
2.主要功能:
日志记录
性能统计
安全控制
事物处理
异常处理
3.advice类型:
前置通知(before advice)
返回后通知(after returning advice)
抛出异常后通知(after throwing advice)
后通知(after advice)
环绕通知(around advice)
范例
例子我是基于maven和Spring注解的方式,用POJO实现
工程的主要文件的配置
pom.xml
4.0.0
imooc
springmaven
war
0.0.1-SNAPSHOT
springmaven Maven Webapp
http://maven.apache.org
SSHMJ-FRANK
mav
sss
http://mvnrepository.com
true
false
default
org.springframework
spring-aop
3.1.1.RELEASE
org.springframework
spring-asm
3.1.1.RELEASE
org.springframework
spring-aspects
3.1.1.RELEASE
org.springframework
spring-beans
3.1.1.RELEASE
org.springframework
spring-context
3.1.1.RELEASE
org.springframework
spring-context-support
3.1.1.RELEASE
org.springframework
spring-core
3.1.1.RELEASE
org.springframework
spring-expression
3.1.1.RELEASE
org.springframework
spring-instrument
3.1.1.RELEASE
org.springframework
spring-instrument-tomcat
3.1.1.RELEASE
org.aspectj
aspectjweaver
1.6.9
commons-pool
commons-pool
1.5.3
commons-collections
commons-collections
3.2
log4j
log4j
1.2.16
org.springframework
spring-jms
3.1.1.RELEASE
org.springframework
spring-oxm
3.1.1.RELEASE
org.springframework
spring-web
3.1.1.RELEASE
org.springframework
spring-webmvc
3.1.1.RELEASE
org.springframework
spring-webmvc-portlet
3.1.1.RELEASE
org.springframework
spring-struts
3.1.1.RELEASE
commons-httpclient
commons-httpclient
3.1
ognl
ognl
2.6.9
javax.servlet
javax.servlet-api
3.0.1
javax.servlet
jstl
1.2
cglib
cglib
2.2
org.apache.maven.plugins
maven-site-plugin
3.4
springmaven
`
web.xml`
springmaven
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
spring
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-servlet.xml
2
spring
*.html
applicationContext.xml
spring-servlet.xml
IToDo.java
切入点(Pointcut)接口
package com.service.imp;
public interface IToDo {
public String toEat();
}
ToDo.java
package com.service;
import org.springframework.stereotype.Service;
import com.service.imp.IToDo;
@Service
public class ToDo implements IToDo {
@Override
public String toEat() {
System.out.println("吃苹果");
return "吃苹果";
}
}
IPreDo.java
package com.service.imp;
public interface IPreDo {
public String toPre();
}
application.java
用于测试
package springmaven;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.imp.IToDo;
public class application {
public static void main(String[] args) {
ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");
IToDo tdo = (IToDo)appCtx.getBean("toDo");
tdo.toEat();
}
}
工程图片
返回的结果
洗手
吃苹果
会自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面,我这里没有使用注解的方式,使用了xml配置的方式。
就是用来配置aspectJ切面
proxy-target-class:
设置代理模式。当poxy-target-class="true"
时,表示使用CGLib动态代理技术织入增强。设置为false时,表示使用jdk动态代理织入增强,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。
设置切面,ref是切面Bean的id名
这里设置切入点,expression设置切面植入的切入点的方法地址
在执行切入点方法之前执行切面方法,method为切面中的执行方法,pointcut-ref与切点的id一致就可以了
定义一个AOP通知者
后通知
返回后通知
抛出后通知
周围通知
定义一个切面
前通知
顶级配置元素,类似于
这种东西 定义一个切点
returning:
是切点的返回值,在切面中通过returnValue接受,若要获取传给切点的参数在切面的java方法参数中加上JoinPoint,获取的是一个数组,按顺序排列。
package com.service;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Service;
import com.service.imp.IPreDo;
@Service
public class PreDo implements IPreDo {
@Override
public String toPre(JoinPoint joinPoint,Object returnValue) {
System.out.println("洗手");
System.out.println(joinPoint.getArgs()[0]);
return "洗手";
}
}