AOP介绍
AOP又称为面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
简单点说,AOP就是在不改变原有代码的情况下,横向切入代码,以补充业务(本质上是采用了动态代理模式)
AOP实现
1、首先导入jar包(Maven项目)
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
2、实现的方式有以下三种
接口以及实现类
package com.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
package com.service;
public class UserServiceImp implements UserService {
public void add() {
System.out.println("add");
}
public void delete() {
System.out.println("delete");
}
public void query() {
System.out.println("query");
}
public void update() {
System.out.println("update");
}
}
然后去写我们的增强类 , 我们编写两个 , 一个前置增强 一个后置增强
package com.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
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() + "方法被执行了");
}
}
package com.log;
public class AfterLog implements AfterReturningAdvice {
//returnVaule: 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了" + target.getClass().getName()
+"的"+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/beanss
https://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.service.UserServiceImp"></bean>
<bean id="log" class="com.log.Log"/>
<bean id="afterlog" class="com.log.AfterLog"/>
<!--配置aop-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置)-->
<aop:pointcut id="point" expression="execution(* com.service.UserServiceImp.*(..))"/>
<!--执行环绕-->
<aop:advisor advice-ref="log" pointcut-ref="point"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="point"/>
</aop:config>
</beans>
测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applcationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userservice");
userService.add();
}
}
自定义一个切入类
package com.diy;
public class DiyPointcut {
public void before(){
System.out.println("before");
}
public void after(){
System.out.println("after");
}
}
设置配置文件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
https://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.service.UserServiceImp"></bean>
<bean id="log" class="com.log.Log"/>
<bean id="afterlog" class="com.log.AfterLog"/>
<bean id="diy" class="com.diy.DiyPointcut">
</bean>
<aop:config>
<!--自定义切面-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.service.UserServiceImp.*(..))"/>
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
测试
public class Test {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
自定义切入类
package com.service;
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 //标注这个类是一个切面
public class AnnotationPointcut {
@Before("execution(* com.service.UserServiceImp.*(..))")
public void before(){
System.out.println("before");
}
@After("execution(* com.service.UserServiceImp.*(..))")
public void after(){
System.out.println("after");
}
//在环绕增强中,我们可以给around方法一个参数,代表我们要获取切入的点
@Around("execution(* com.service.UserServiceImp.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("around");
Object proceed = joinPoint.proceed();
System.out.println("after around");
}
}
设置配置文件信息(注意要开启注解支持)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--支持注解-->
<aop:aspectj-autoproxy/>
<!--注册bean-->
<bean id="userservice" class="com.service.UserServiceImp"></bean>
<bean id="annotationPointcut" class="com.diy.AnnotationPointcut"></bean>
</beans>