AOP
AOP
10,代理模式
10.1,静态代理
角色分析:
- 抽象角色:一般会使用接口或者抽象类来解决
- 真实角色︰被代理的角色
- 代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作
- 客户:访问代理对象的人!
代码步骤:
-
接口
//租房 public interface Rent { public void rent(); }
-
真实角色
//房东 public class Host implements Rent { @Override public void rent() { System.out.println("房东要出租房子"); } }
-
代理角色
//代理 public class Proxy implements Rent{ //组合 组合优于继承 private Host host; public Proxy() { } public Proxy(Host host) { this.host = host; } @Override public void rent() { seeHouse(); host.rent(); fare(); } //看房 public void seeHouse(){ System.out.println("中介带你看房"); } //收中介费 public void fare(){ System.out.println("收中介费"); } }
-
客户端访问代理角色
public class Client { public static void main(String[] args) { //房东出租房子 Host host = new Host(); //代理 中介帮助出租房子,一般会有一些附属操作! Proxy proxy = new Proxy(host); //你不用面对房东,直接找中介租房即可 proxy.rent(); } }
代理模式的好处:
- 可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
- 公共也就就交给代理角色!实现了业务的分工!
- 公共业务发生扩展的时候,方便集中管理!
缺点:
- 一个真实角色就会产生一个代理角色;代码量会翻倍~~开发效率变低
10.2 动态代理
- 动态代理和静态代理角色一样
- 动态代理的代理类是动态生成的,不是我们直接写好的!
- 动态代理分为两大类:基于接口的动态代理,基于类的动态代理
- 基于接口—-JDK动态代理【使用这个】
- 基于类:dglib
- java字节码实现:javassist
了解两个类:proxy:代理,lnvocationHandler:调用处理程序
动态代理好处:
- 可以使真实角色的操作更加纯粹!不用去关注一些公共的业务
- 公共也就就交给代理角色!实现了业务的分工!
- 公共业务发生扩展的时候,方便集中管理!
- 一个动态代理类代理的是一个接口,一般就是对应的一类业务
- 一个动态代理类可以代理多个类,只要是实现了同一个接口即可!
简单实现:
真实角色
//房东
public class Host implements Rent {
@Override
public void rent() {
System.out.println("房东要出租房子");
}
}
接口
//租房
public interface Rent {
public void rent();
}
lnvocationHandler
//利用这个类生成代理类《动态生成》
public class proxyInvocationHandler implements InvocationHandler {
/*Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
new Class<?>[] { Foo.class },
handler); */
//被代理的接口
private Object rent;
public void setRent(Rent rent) {
this.rent = rent;
}
//生成得到代理对象类
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this);
}
//处理代理实例,并返回结果
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//动态代理的本质,就是使用反射机制执行
Object result = method.invoke(rent,args);
return result;
}
}
Client
public class Client {
public static void main(String[] args) {
//真实角色
Host host = new Host();
proxyInvocationHandler handler = new proxyInvocationHandler();
//通过调用程序处理角色来处理我们要调用的接口对象
handler.setRent(host);
Rent proxy = (Rent) handler.getProxy();
//这里的代理是动态生成的,我们并没有写
proxy.rent();
}
}
11.3,使用Spring实现AOP
【导包】导入依赖包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
方式一:使用Spring的API接口【主要是Spring接口实现】
接口:
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
接口实现类:
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 select() {
System.out.println("查询了");
}
}
增加类:
前置日志:
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()+"被执行了");
}
}
后置日志:
public class AfterLog implements AfterReturningAdvice {
/*
* returnValue:返回值
* */
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnValue);
}
}
Spring配置文件:
<?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
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册Bean-->
<bean id="userService" class="service.UserServiceImpl"/>
<bean id="log" class="log.Log"/>
<bean class="log.AfterLog" id="afterLog"/>
<!--方式一:使用原生的SpringApi接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:expression:表达式execution(要执行的位置 * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试类:
public class MyTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理的是一个接口:注意点
UserService bean = context.getBean("userService", UserService.class);
bean.add();
bean.select();
}
}
方式二:自定义实现AOP【主要是定义切面】
在方式一的基础上,增加一个diy类【自定义的类】
public class DiyPointCut {
public void before(){
System.out.println("=====方法执行前=======");
}
public void after(){
System.out.println("++++++++方法执行后+++++++++");
}
}
配置文件:写在xml中
<bean id="diy" class="diy.DiyPointCut"/>
<aop:config>
<!-- 自定义切面,ref 要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
方式二没有方式一强大
方式三:使用注解实现
注解实现:
@Aspect //标注这个类为一个切面
public class Annotation {
@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 joinPoint) throws Throwable {
System.out.println(joinPoint);
System.out.println("=========方法环绕前+++++++++");
Object proceed = joinPoint.proceed();
System.out.println("=========方法环绕后+++++++++");
System.out.println(proceed);
}
}
<!--方式3:使用注解-->
<bean class="diy.Annotation" id="annotation"/>
<!--开启注解支持 JDK(默认proxy-target-class="false") cglib (proxy-target-class="true" )-->
<aop:aspectj-autoproxy />
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术