角色分析:
抽象角色:
-般会使用接口或者抽象类来解决
真实角色:
被代理的角色
代理角色:
代理真实角色,代理真实角色后,我们一般会做一些附属操作
客户:
访问代理对象的人!
代理模式的好处:
可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
公共也就就交给代理角色!实现了业务的分工!
公共业务发生扩展的时候,方便集中管理!
缺点:
一个真实角色就会产生一 个代理角色;代码量会翻倍开发效率会变低~
代码步骤:
1.接口
2.真实角色
3.代理角色
4.客户端访问代理角色
======================================
动态代理和静态代理角色一样
动态代理的代理类是动态生成的,不是我们直接写好的!
动态代理分为两大类:基于接口的动态代理,基于类的动态代理
。基于接口--- jJDK动态代理[ 我们在这里使用]
。基于类: cglib
。java字节码实现: javasist
需要了解两个类:
Proxy: 代理
InvocationHandler: 调用处理程序
动态代理的好处:
可以使真实角色的操作更加纯粹!不用去关注一 -些公共的业务
公共也就就交给代理角色!实现了业务的分工! .
公共业务发生扩展的时候,方便集中管理!
一个动态代理类代理的是一个接口,一般就是对应的一类业务
一个动态代理类可以代理多个类,只要是实现了同一个接口即可!
=======================================================
在pom.xml导入
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
方式一:用Spring API接口
1.建一个接口、一个实现类
2.建一个log包
3.java类
AfterLog.java
public class AfterLog implements AfterReturningAdvice {
//o(returnValue)返回值
//method:要执行的目标对象的方法
//objects:参数
//o1(target):目标对象
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果为"+o);
}
}
BeforeLog.java
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//objects:参数
//o(target):目标对象
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
xml里注册bean
<bean id="userService" class="com.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.log.BeforeLog"/>
<bean id="afterLog" class="com.log.AfterLog"/>
配置AOP,需要导入约束
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
配置AOP,需要导入约束
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置!修饰符 返回值 类型 方法名 参数) -->
<aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加! -->
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
</aop:config>
完整的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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/sp ring-beans.xsd
http://www.springframework.org/schema/context
https://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="userService" class="com.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.log.BeforeLog"/>
<bean id="afterLog" class="com.log.AfterLog"/>
<!-- 方式一:使用原生Spring API接口-->
<!-- 配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:expression:表达式,execution(要执行的位置!修饰符 返回值 类型 方法名 参数) -->
<aop:pointcut id="pointcut" expression="execution(* com.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加! -->
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
4.测试
动态代理代理的是接口UserService不是实现类
UserService userService=context.getBean("userService", UserService.class);
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserService userService=context.getBean("userService", UserService.class);
userService.add();
}
方式二:用自定义类实现AOP接口
1.建个自定义包
2.建一个自定义类
3.DiyPointCut.java类
public class DiyPointCut {
public void Before(){
System.out.println("方法前");
}
public void After(){
System.out.println("方法后");
}
}
<!-- 方式二:自定义类-->
<bean id="diy" class="com.Diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:after method="After" pointcut-ref="point"/>
<aop:before method="Before" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
配置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 id="userService" class="com.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.log.BeforeLog"/>
<bean id="afterLog" class="com.log.AfterLog"/>
<!-- 方式二:自定义类-->
<bean id="diy" class="com.Diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:after method="After" pointcut-ref="point"/>
<aop:before method="Before" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
</beans>
4.测试–测试类不变
方式三:用注解
1.建一个注解类
2.AnnotationPointCut.java类
@Aspect
public class AnnotationPointCut {
@Before("execution(* com.service.UserServiceImpl.*(..))")
public void Before(){
System.out.println("方法前================");
}
@After("execution(* com.service.UserServiceImpl.*(..))")
public void After(){
System.out.println("方法后===============");
}
}
@Aspect 标注他是一个切入面的类
3.XML配置
<!-- 方式三:注解-->
<bean id="annotationPointCut" class="com.Diy.AnnotationPointCut"/>
<!-- 开启注解支持-->
<aop:aspectj-autoproxy/>
完整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 id="userService" class="com.service.UserServiceImpl"/>
<bean id="beforeLog" class="com.log.BeforeLog"/>
<bean id="afterLog" class="com.log.AfterLog"/>
<!-- 方式三:注解-->
<bean id="annotationPointCut" class="com.Diy.AnnotationPointCut"/>
<!-- 开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>
4.测试–测试类不变
注解环绕
public class AnnotationPointCut {
@Before("execution(* com.service.UserServiceImpl.*(..))")
public void Before() {
System.out.println("方法前================");
}
@After("execution(* com.service.UserServiceImpl.*(..))")
public void After() {
System.out.println("方法后===============");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点;(少用)
@Around("execution(* com.service.UserServiceImpl.*(..))")
public void Around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
// 执行方法
Object proceed = jp.proceed();
System.out.println("环绕后");
}
}
执行结果:
环绕前
方法前================
删除了一个用户
方法后===============
环绕后