1.AOP的概念
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。
2.AOP的作用
允许用户自定义切面,在不改变原有代码的基础上,实现增加新功能。
- 横切关注点:跨越应用程序多个模块的方法或功能,即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点,如日志,安全,缓存,事务等等…
- 切面(ASPECT):横切关注点被模块化的特殊对象,即,它是一个类。
- 通知(Advice):切面必须是要完成的工作,即,它是类中的一个方法。
- 目标(Target):被通知的对象。
- 代理(Proxy):向目标对象应用通知之后创建的对象。
- 切入点(PointCut):切面通知执行的“地点”的定义。
- 连接点(jointPoint):与切入点匹配的执行点。
在AOP中,通过Advice定义切面逻辑,Spring中支持5中类型的Advice:
3.AOP的实现
导包
使用AOP之前,先要导入依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
idear导包:
配置AOP
- 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"
xmlns:aop="http://www.springframework.org/schema/aop"//需要导入的AOP约束
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
-
AOP的配置
方法一:使用Spring的API接口
<!-- 注册bean-->
<bean id="userService" class="com.ming.service.UserServiceImpl"/>
<bean id="log" class="com.ming.log.Log"/>
<bean id="afterLog" class="com.ming.log.AferLog"/>
<!-- 配置AOP:需要导入的AOP约束-->
<aop:config>
<!-- 切入点:expression:表达式,execution:(要执行的位置!* * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.ming.service.UserServiceImpl.*(..))"/>
<!-- 执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
方法二:自定义一个类来实现AOP
<bean id="diy" class="com.ming.diy.DiyPointCut"/>
<aop:config>
<!-- aspect:切面,要引入的类-->
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.ming.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>//插入切入点前
<aop:after method="after" pointcut-ref="point"/>//插入切入点后
</aop:aspect>
</aop:config>
方法三:使用注解
<bean id="annotationPointCut" class="com.ming.diy.AnnotationPointCut"/>
<!-- 开启注解支持 -->
<aop:aspectj-autoproxy/>
案例
在业务类前后插入日志:
业务类接口
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
业务类实现
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("更新了一个用户");
}
public void select() {
System.out.println("查询了一个用户");
}
}
1).使用Spring的API接口实现
- 前日志类
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
- 后日志类
public class AferLog implements AfterReturningAdvice {
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] args) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果为:"+returnValue);
}
}
- 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean-->
<bean id="userService" class="com.ming.service.UserServiceImpl"/>
<bean id="log" class="com.ming.log.Log"/>
<bean id="afterLog" class="com.ming.log.AferLog"/>
<!-- 配置AOP:需要导入的AOP约束-->
<aop:config>
<!-- 切入点:expression:表达式,execution:(要执行的位置!* * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.ming.service.UserServiceImpl.*(..))"/>
<!-- 执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 测试
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)context.getBean("userService");
userService.select();
}
注意:由于动态代理的是接口,所有getBean取的应该是接口类
- 运行结果
2)自定义一个类来实现AOP
- 自定义日志类
public class DiyPointCut {
public void before(){
System.out.println("=====方法执行前=====");
}
public void after(){
System.out.println("=====方法执行后=====");
}
}
- 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="diy" class="com.ming.diy.DiyPointCut"/>
<aop:config>
<!-- aspect:切面,要引入的类-->
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.ming.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>//插入切入点前
<aop:after method="after" pointcut-ref="point"/>//插入切入点后
</aop:aspect>
</aop:config>
</beans>
- 测试同上,运行结果
3)使用注解来实现AOP
- 切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.w3c.dom.ProcessingInstruction;
@Aspect//标注这是一个切面
public class AnnotationPointCut {
@Before("execution(* com.ming.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=====方法执行前=====");
}
@After("execution(* com.ming.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=====方法执行后======");
}
@Around("execution(* com.ming.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Signature signature=jp.getSignature();//获得签名
System.out.println("signature:"+signature);
//执行方法
Object proceed=jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
- 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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="diy" class="com.ming.diy.DiyPointCut"/>
<bean id="annotationPointCut" class="com.ming.diy.AnnotationPointCut"/>
<!-- 开启注解支持 -->
<aop:aspectj-autoproxy/>
</beans>
- 测试同上,运行结果