1.什么是AOP?
Aop(Aspect Oriented Programming)意为面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同意维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范围,利用AOP可以对业务逻辑的各个部分进行隔离,从而是的业务逻辑各部分之间的耦合度降低,提供程序的可重用性,同时提高了开发效率。
2.AOP在spring中的作用
- 横切关注点:跨越应用程序多个模块的方法或功能,与我们业务无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全、事务、缓存等。
- 切面(Aspect):横切关注点被模块化的特殊对象,它是一个类。
- 通知(Advice):切面必须要完成的工作。它是类中的一个方法。
- 目标(Target):被通知对象。
- 代理(Proxy):向目标对象应用通知之后创建的对象。
- 切入点(PointCut):切面通知执行的“地点”的定义
- 连接点(JoinPoint):与切入点匹配的执行点。
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5中类型的Advice
通知类型 | 连接点 |
前置通知 | 方法前 |
后置通知 |
方法后 |
环绕通知 | 方法前后 |
异常抛出通知 | 方法抛出异常 |
引介通知 | 类中增加新的方法属性 |
即AOP在不改变原有带啊的情况下,去增加新的功能
3.使用Spring实现AOP
重点;使用AOP织入,需要导入一个依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
方式一:使用springAPI接口
接口
package com.yl.pojo;
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
实现类:
package com.yl.pojo;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void query() {
System.out.println("查询了一个用户");
}
}
log:
package com.yl.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class log implements MethodBeforeAdvice {
@Override
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
afterLog:
package com.yl.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnValue);
}
}
applicationContext.xml
将实现类和日志注入spring,并且需要导入aop约束
<?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-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean-->
<bean id="userService" class="com.yl.pojo.UserServiceImpl"></bean>
<bean id="log" class="com.yl.log.log"></bean>
<bean id="afterLog" class="com.yl.log.AfterLog"/>
<!-- 方式一:使用原生的SpringAPI接口-->
<!-- 配置AOP,需要导入AOP约束xmlns:aop="http://www.springframework.org/schema/aop"+ http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd-->
<aop:config>
<!-- 切入点expression:表达式,execution(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* com.yl.pojo.UserServiceImpl.*(..))"/>
<!-- 执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试类:
import com.yl.pojo.UserService;
import com.yl.pojo.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
结果:
com.yl.pojo.UserServiceImpl的add被执行了
增加了一个用户
执行了add方法,返回结果为null
4.自定义来实现AOP(切面定义)
applicationContext.xml
<!-- 方式2:自定义类-->
<bean id="diy" class="com.yl.diy.DiyPointCut"/>
<aop:config>
<!-- 自定义切面-->
<aop:aspect ref="diy">
<!-- 切入点execution(*返回类型 *拦截的包名)-->
<aop:pointcut id="point" expression="execution(* com.yl.pojo.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
diy类
package com.yl.diy;
public class DiyPointCut {
public void before(){
System.out.println("==========方法执行前=========");
}
public void after(){
System.out.println("=============方法执行后=============");
}
}
测试类:
import com.yl.pojo.UserService;
import com.yl.pojo.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
结果
==========方法执行前=========
增加了一个用户
=============方法执行后=============
3.注解实现AOP
applicationContext.xml
<bean id="annotationPoinCut" class="com.yl.diy.AnnotationPointCut"/>
<!-- proxy-target-class="false(JDK默认) true(cglib)-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
自定义类
package com.yl.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect//标注这个类是一个切面
//使用注解方式实现AOP
public class AnnotationPointCut {
@Before("execution(* com.yl.pojo.UserServiceImpl.*(..))")
public void before(){
System.out.println("======方法执行前======");
}
@After("execution(* com.yl.pojo.UserServiceImpl.*(..))")
public void after(){
System.out.println("======方法执行后======");
}
//在环绕增强中,可以给定一个参数,代表可以获取处理切入的点
@Around("execution(* com.yl.pojo.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("执行前");
//执行方法
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
测试类
import com.yl.pojo.UserService;
import com.yl.pojo.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = context.getBean("userService",UserService.class);
userService.add();
}
}
结果
执行前
======方法执行前======
增加了一个用户
环绕后
null
======方法执行后======