原例来源:尚硅谷Spring5框架教程(idea版)
(1)面向切面编程(方面),利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
(2)通俗描述:不通过修改源代码方式,在主干功能里面添加新功能
功能模块可插入可拔出
有两种情况动态代理
1、使用 JDK 动态代理,使用 Proxy 类里面的方法创建代理对象
方法有三个参数:
第一参数,类加载器
第二参数,增强方法所在的类,这个类实现的接口,支持多个接口
第三参数,实现这个接口 InvocationHandler,创建代理对象,写增强的部分
package com.company.demo4;
public interface UserDao {
int add(int a, int b);
String update(String id);
}
package com.company.demo4;
public class UserDaoImpl implements UserDao {
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public String update(String id) {
return id;
}
}
Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new InvocationHandler());
package com.company.demo4;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class JDKProxy {
public static void main(String[] args) {
Class[] interfaces = {
UserDao.class};
// 方法一:
// Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces,new InvocationHandler() {
// @Override
// public Object invoke(Object proxy, Method method, Object[] args)
// throws Throwable {
// return null;
// }
// });
// 方法二:
UserDao userDao = new UserDaoImpl();
//
UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
int result = dao.add(1, 2);
System.out.println("result:"+result);
}
}
// 方法二的第三个参数:
//创建代理对象代码
class UserDaoProxy implements InvocationHandler {
private Object obj;
//1 把创建的是谁的代理对象,把谁传递过来
//有参数构造传递
public UserDaoProxy(Object obj) {
this.obj = obj;
}
//增强的逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前执行...."+method.getName()+" :传递的参数..."+ Arrays.toString(args));
//被增强的方法执行
Object res = method.invoke(obj, args);
//方法之后
System.out.println("方法之后执行...."+obj);
return res;
}
}
类里面哪些方法可以被增加,这些方法称为连接点
实际被增强的方法,称为切入点
实际增强的逻辑部分称为通知(或称为增强)
通知的类型:前置通知、后置通知、环绕通知、 异常通知、最终通知
切面是一种动作,把通知应用到切入点过程
(1)AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作
(1)基于 xml 配置文件实现
(2)基于注解方式实现
(一般使用注解方式实现)
(1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构:
execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )
举例 1:对com.company.BookDao
类里面的 add 进行增强
execution(* com.company.BookDao.add(..))
举例 2:对 com.company.BookDao
类里面的所有的方法进行增强
execution(* com.company.BookDao.* (..))
举例 3:对com.company.BookDao
包里面所有类,类里面所有方法进行增强
execution(* com.company.BookDao.*.* (..))
package com.company.aop.AspectJ;
public class User {
public void add() {
System.out.println("add.......");
}
}
(1)在增强类里面,创建方法,让不同方法代表不同通知类型
package com.company.aop.AspectJ;
//增强的类
public class UserProxy {
public void before() {
//前置通知
System.out.println("before......");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.company.aop.AspectJ">context:component-scan>
beans>
package com.company.aop.AspectJ;
import org.springframework.stereotype.Component;
@Component
public class User {
public void add() {
System.out.println("add.......");
}
}
package com.company.aop.AspectJ;
import org.springframework.stereotype.Component;
//增强的类
@Component
public class UserProxy {
public void before() {
//前置通知
System.out.println("before......");
}
}
package com.company.aop.AspectJ;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
//增强的类
@Component
@Aspect //生成代理对象
public class UserProxy {
public void before() {
//前置通知
System.out.println("before......");
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.company.aop.AspectJ">context:component-scan>
<aop:aspectj-autoproxy>aop:aspectj-autoproxy>
beans>
(1)在增强类的里面,在作为通知方法上面添加通知类型注解,使用切入点表达式配置
package com.company.aop.AspectJ;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强的类
@Component
@Aspect //生成代理对象
public class UserProxy {
//前置通知
//@Before 注解表示作为前置通知
@Before(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void before() {
System.out.println("before.........");
}
}
package com.company.aop.AspectJ;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testAop {
@Test
public void testAopAsp(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
User user = context.getBean("user", User.class);
user.add();
}
}
package com.company.aop.AspectJ;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强的类
@Component
@Aspect //生成代理对象
public class UserProxy {
//前置通知
//@Before 注解表示作为前置通知
@Before(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void before() {
System.out.println("前置通知.........");
}
//后置通知(返回通知)
@AfterReturning(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void afterReturning() {
System.out.println("后置通知.........");
}
//最终通知
@After(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void after() {
System.out.println("最终通知.........");
}
//异常通知
@AfterThrowing(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void afterThrowing() {
System.out.println("异常通知.........");
}
//环绕通知
@Around(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws
Throwable {
System.out.println("环绕之前.........");
//被增强的方法执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后.........");
}
}
package com.company.aop.AspectJ;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强的类
@Component
@Aspect //生成代理对象
public class UserProxy {
//相同切入点抽取
@Pointcut(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void pointdemo() {
}
//前置通知
//@Before 注解表示作为前置通知
@Before(value = "pointdemo()")
public void before() {
System.out.println("前置通知.........");
}
//后置通知(返回通知)
@AfterReturning(value = "pointdemo()")
public void afterReturning() {
System.out.println("后置通知.........");
}
//最终通知
@After(value = "pointdemo()")
public void after() {
System.out.println("最终通知.........");
}
//异常通知
@AfterThrowing(value = "pointdemo()")
public void afterThrowing() {
System.out.println("异常通知.........");
}
//环绕通知
@Around(value = "pointdemo()")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws
Throwable {
System.out.println("环绕之前.........");
//被增强的方法执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后.........");
}
}
在增强类上面添加注解 @Order(数字类型值)
,数字类型值越小优先级越高
例子:创建PersonProxy
类
package com.company.aop.AspectJ;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(3)
public class PersonProxy {
//前置通知
//@Before 注解表示作为前置通知
@Before(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void before() {
System.out.println("PersonProxy 的 前置通知.........");
}
}
package com.company.aop.AspectJ;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//增强的类
@Component
@Aspect //生成代理对象
//(1)在增强类上面添加注解 @Order(数字类型值),数字类型值越小优先级越高
@Order(1)
public class UserProxy {
//前置通知
//@Before 注解表示作为前置通知
@Before(value = "execution(* com.company.aop.AspectJ.User.add(..))")
public void before() {
System.out.println("UserProxy 的 前置通知.........");
}
}
package com.company.aop.aopxml;
public class Book {
public void buy(){
System.out.println("buy Book");
}
}
package com.company.aop.aopxml;
public class BookProxy {
public void before(){
System.out.println("before BookProxy");
}
}
<bean id="book" class="com.company.aop.aopxml.Book">bean>
<bean id="bookProxy" class="com.company.aop.aopxml.BookProxy">bean>
<aop:config>
<aop:pointcut id="p" expression="execution(* com.company.aop.aopxml.Book.buy(..))"/>
<aop:aspect ref="bookProxy">
<aop:before method="before" pointcut-ref="p"/>
aop:aspect>
aop:config>
bean.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
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/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="book" class="com.company.aop.aopxml.Book">bean>
<bean id="bookProxy" class="com.company.aop.aopxml.BookProxy">bean>
<aop:config>
<aop:pointcut id="p" expression="execution(* com.company.aop.aopxml.Book.buy(..))"/>
<aop:aspect ref="bookProxy">
<aop:before method="before" pointcut-ref="p"/>
aop:aspect>
aop:config>
beans>
package com.company.aop.AspectJ;
import com.company.aop.aopxml.Book;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testAop {
@Test
public void testAopAsp(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Book book = context.getBean("book", Book.class);
book.buy();
}
}
改为完全注解开发方式,不需要配置文件
@Configuration
@ComponentScan(basePackages = {
"com.company"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}