惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
博客园 - 司徒正美
A
About on SuperTechFans
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
I
InfoQ
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
F
Fortinet All Blogs
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
D
Docker
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
H
Help Net Security
量子位
IT之家
IT之家

博客园 - 风来风往风伤(TiAmo)

CPU监控工具 桌面日历软件 no matching host key type found. Their offer: ssh-rsa报错信息具体方法 js漂浮代码 ThoughtWorks(中国)程序员读书雷达 Java实现文件的RSA和DES加密算法 二维码 编码原理简介 Maven+eclipse+svn项目构建 QtCreator中使用CMake+Ninja加速编译 error C1088: 无法刷新编译器中间文件 error C1041: 无法打开程序数据库“xxx\vc140.pdb”;如果要将多个 CL.EXE 写入同一个 .PDB 文件 - 风来风往风伤(TiAmo) Navicat key MICROSOFT SQL SERVER 2012 序列号 IT外包概要 RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set. 判断django中的orm为空 QEventLoop配合QTimer实现阻塞任务超时处理 sql调优的总结 2019Python学习路线图
Spring_AOP 原理 增强类型
风来风往风伤(TiAmo) · 2022-05-11 · via 博客园 - 风来风往风伤(TiAmo)
Spring AOP底层以CGLib或者JDK代理来实现。

实现了AOP联盟的Advice接口

主要包括:前置增强、后置增强、环绕增强、、异常抛出增强,引介增强。

代码实现步骤:
1创建目标类,要求实现目标接口。
2创建增强类,
3创建代理工厂ProxyFactory
4将目标类,增强类设置到代理工厂
5用代理工厂生成代理实例,调用目标方法。

前置增强例子:
package com.spring.aop;

public interface Waiter {
public void  greatTo(String name);
public void serveTo(String name);

}
--------------------------------------------------------------------------------
package com.spring.aop;

public class NavicateWaiter implements Waiter{

@Override
public void greatTo(String name) {
System.out.println("great to"+name);
}

@Override
public void serveTo(String name) {
System.out.println("serving..."+name);
}

}

--------------------------------------------------------------------------------
前置增强
package com.spring.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;//增强类接口

public class GreetingBeforeAdvice  implements MethodBeforeAdvice{

@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
String name=(String) arg1[0];
System.out.println("hello,"+name);
}

}

--------------------------------------------------------------------------------
package com.spring.aop;

import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;//代理工程

public class WaiterTest {
public static void main(String[] args) {
BeforeAdvice advice=new GreetingBeforeAdvice();
Waiter target=new NavicateWaiter();
ProxyFactory pf=new ProxyFactory();
pf.setTarget(target);
pf.addAdvice(advice);
 
Waiter waiter=(Waiter) pf.getProxy();
waiter.greatTo("zhaoming");
}
}
--------------------------------------------------------------------------------
环绕增强
public class GreetingAdvice implements MethodInterceptor{

public Object invoke(MethodInvocation invocation) throws Throwable {
Object[] args=invocation.getArguments();
String name=(String) args[0];
System.out.println("Welcome to!"+name);
Object obj=invocation.proceed();
System.out.println("next time see you !");
return obj;
}

}
--------------------------------------------------------------------------------
配置文件引用:
<?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:p="http://www.springframework.org/schema/p" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
                     http://www.springframework.org/schema/beans/spring-beans.xsd 
                     http://www.springframework.org/schema/tx 
                     http://www.springframework.org/schema/tx/spring-tx.xsd 
                     http://www.springframework.org/schema/aop 
                     http://www.springframework.org/schema/aop/spring-aop.xsd"> 

<bean id="greetingAround" class="com.spring.myadvice.GreetingAdvice"></bean>
<bean id="target" class="com.spring.mybean.imp.NavicateWaiter"></bean>
<bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
p:interfaces="com.spring.mybean.Waiter"
p:target-ref="target"
p:interceptorNames="greetingAround"
p:proxyTargetClass="true"
/>
</beans>