AOP原理之jdk代理 & cglib代理

jdk代理就是代理有接口的类

cglib代理是代理没有接口的类

代理就是拦截目标对象的所有方法,在方法前后执行一些代码,参考java之proxy

==========================jdk代理=================================

接口类:

public interface Waiter {
public void greetTo(String name);
public void serverTo(String name);
}

实现接口类:

public class WaiterImp implements Waiter{
@Override
public void greetTo(String name) {
System.out.println("greetTo"+name);
}
@Override
public void serverTo(String name) {
System.out.println("serverTo"+name);
}
}

before拦截器:

public class BeforeGreeting implements MethodBeforeAdvice{  //实现接口MethodBeforeAdvice
@Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.print("hello"+arg1[0]);
}
}

after拦截器:

public class AfterAdvice implements AfterReturningAdvice{  //实现接口AfterReturningAdvice
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("bye-bye...");
}
}

前后拦截器:

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class RoundAdvice implements MethodInterceptor{   //实现接口MethodInterceptor
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("begin...");
Object obj = arg0.proceed();    //须要执行这段
System.out.println("end...");
return obj;
}
}

测试类:

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aopBase/bean1.xml");
Waiter waiter = applicationContext.getBean("waiterProxy", Waiter.class);  //接口类对象, 引用waiterProxy代理工厂id值,见下xml配置
waiter.greetTo("Mr zhang");
waiter.serverTo("Mr wang");

xml配置:见最下

==========================cglib代理=================================

无接口类:

public class UserService {
public void sayHello(String name){
System.out.println("sayhello:"+name);
}
}

测试类:

UserService userService = applicationContext.getBean("usProxy", UserService.class); //引用xml中代理工厂的id值
userService.sayHello("Mr zhang");

xml配置:见最下

=============================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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"         //添加

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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop                             //添加
http://www.springframework.org/schema/aop/spring-aop.xsd">   //添加

<!-- jdk代理(有接口) -->
<bean id="waiter" class="aopBase.WaiterImp"/>   //目标对象
<bean id="before" class="aopBase.BeforeGreeting"/>   //拦截器

<!-- 通知 = advice(拦截的方式)+切面(拦截的方法) -->  
<bean id="greetToAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="before">
</property>
<property name="patterns">
<list>
<value>.*greetTo.*</value>
</list>
</property>
</bean>


<!-- <bean id="waiterProxy" class="org.springframework.aop.framework.ProxyFactoryBean"   //代理工厂对象
p:target-ref="waiter"                             //目标对象
p:interceptorNames="before,after"                 //拦截器对象
p:proxyInterfaces="aopBase.Waiter"/>   //代理接口  (只有jdk代理有) -->    //可以是这种--------------------------1)

<bean id="waiterProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="waiter"></property>
<property name="interceptorNames">
<list>
<value>greetToAdvisor</value>
</list>
</property>
</bean>    //也可以是这种------------------------------------------------------------------------------------------------2)


<!-- cglib代理(无接口) -->
<bean id="us" class="aopBase.UserService"/>  //目标对象
<bean id="usProxy" class="org.springframework.aop.framework.ProxyFactoryBean"  //代理工厂对象
p:target-ref="us"  //目标对象
p:interceptorNames="before"   //拦截器对象
p:proxyTargetClass="true"/>   //cglib代理特有语句
</beans>

============================最终简化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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- jdk代理(有接口) -->
<bean id="waiter" class="aopBase.WaiterImp" />
<bean id="before" class="aopBase.BeforeGreeting" />
<bean id="after" class="aopBase.AfterAdvice"></bean>
<bean id="round" class="aopBase.RoundAdvice"></bean>

<!-- 通知 -->
<bean id="greetToAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="before">   //拦截方式
</property>
<property name="patterns">
<list>
<value>.*greetTo.*</value>    //拦截哪个方法
</list>
</property>
</bean>

<!-- 自动代理 -->
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="waiter" />  //拦截哪个类
<property name="interceptorNames">
<list>
<value>greetToAdvisor</value>    //引用
</list>
</property>
</bean>

</beans>

posted @ 2016-06-16 14:03  乱世_独自美  阅读(81)  评论(0)    收藏  举报