Spring_AOP 原理 增强类型

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>

 

posted @ 2022-05-11 11:07  风来风往风伤(TiAmo)  阅读(54)  评论(0编辑  收藏  举报