什么是AOP
AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
AOP在Spring中的作用
提供声明式事务;允许用户自定义切面 ==> 类似于 Java设计模式中的代理模式
了解一些Spring-AOP中的名词
横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ....
切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
目标(Target):被通知对象。
代理(Proxy):向目标对象应用通知之后创建的对象。
切入点(PointCut):切面通知 执行的 “地点”的定义。
连接点(JointPoint):与切入点匹配的执行点。
使用Spring实现AOP
三种方式
- 通过 Spring API接口来实现
- 通过自定义类来实现
- 通过注解来实现
事前准备工作:【重点】
导入AOP依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
业务接口
package service;
public interface Userervice {
public void add();
public void delete();
public void update();
public void select();
}
业务接口实现类
package service;
public class UserserviceImpl implements Userervice{
public void add() {
System.out.println("增加了一个用户");
}
public void delete() {
System.out.println("删除了一个用户");
}
public void update() {
System.out.println("更新了一个用户");
}
public void select() {
System.out.println("查询了一个用户");
}
}
通过Spring API接口实现
- 编写前置日志
package Log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
/*
method:要执行的目标对象的方法
objects : 被调用的方法的参数
o : 目标对象
*/
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName() + "的" + method.getName() + "被执行了");
}
}
- 编写后置日志
package Log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
//returnValue 返回值
//method被调用的方法
//args 被调用的方法的对象的参数
//target 被调用的目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+target.getClass().getName()+"的"+method.getName()+"方法,返回值为"+returnValue);
}
}
- 编写ApplicationContext.xml核心配置文件
【注意:要添加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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--注册bean-->
<bean id="UserService" class="service.UserserviceImpl"/>
<bean id="beforeLog" class="Log.BeforeLog"/>
<bean id="afterLog" class="Log.AfterLog"/>
<-- 方式一:通过Spring API接口实现 -->
<-配置aop-->
<aop:config>
<-- 配置切入点
id:切入点
expression="execution(* service.UserserviceImpl.*(..))":固定的表达式具体用法到用的时候可以去网上查
-->
<aop:pointcut id="point" expression="execution(* service.UserserviceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="point"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="point"/>
</aop:config>
</beans>
- 测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.Userervice;
public class DemoTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Userervice userService = context.getBean("UserService", Userervice.class);
userService.delete();
}
}
通过自定义类实现
- 编写自定义类
package diy;
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
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--注册bean-->
<bean id="UserService" class="service.UserserviceImpl"/>
<bean id="beforeLog" class="Log.BeforeLog"/>
<bean id="afterLog" class="Log.AfterLog"/>
<!-- 方式二:通过自定义切入点-->
<bean id="diypoint" class="diy.diyPointCut"/>
<aop:config>
<aop:aspect ref="diypoint">
<aop:pointcut id="diypointcut" expression="execution(* service.UserserviceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="diypointcut"/>
<aop:after method="after" pointcut-ref="diypointcut"/>
</aop:aspect>
</aop:config>
</beans>
3. 测试类【三种方式的测试类一样】
通过注解实现
- 创建注解实现类
package annotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class AnnotationPoint {
@Before("execution(* service.UserserviceImpl.*(..))")
public void before(){
System.out.println("方法执行前");
}
@After("execution(* service.UserserviceImpl.*(..))")
public void after(){
System.out.println("方法执行后");
}
@Around("execution(* service.UserserviceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
Object proceed = jp.proceed();//执行目标方法proceed
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
http://www.springframework.org/schema/aop/spring-aop.xsd
">
<!--注册bean-->
<bean id="UserService" class="service.UserserviceImpl"/>
<bean id="beforeLog" class="Log.BeforeLog"/>
<bean id="afterLog" class="Log.AfterLog"/>
<!-- 方式三:通过注解 -->
<bean id="annotation" class="annotation.AnnotationPoint"/>
<aop:aspectj-autoproxy/>
</beans>
- 测试类
- 通过aop命名空间的
<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了- <aop:aspectj-autoproxy />有一个
proxy-target-class属性,默认为false,表示使用jdk动态代理注入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术注入增强。- 不过如果目标类没有声明接口,即使
proxy-target-class设置为false,spring也将自动使用CGLib动态代理。