基于接口和基于子类的动态代理

一:基于接口(JDK1.3之后提供):

import com.aiitec.service.IAccountService;
import com.aiitec.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class AccountServiceFactory {
@Autowired
@Qualifier("accountService")
private IAccountService service;//被代理对象,必须实现至少一个接口

@Autowired
private TransactionManager transactionManager;//事务控制器

  
/**
* 获取代理对象
* @return
*/
    public IAccountService getProxy() {
return (IAccountService) Proxy.newProxyInstance(service.getClass().getClassLoader(), service.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result=null;
try{
transactionManager.beginTransaction();
System.out.println("前置通知生效。。。");

result=method.invoke(service,args);

transactionManager.commit();
System.out.println("后置通知生效。。。");
}catch (Exception e){
transactionManager.rollback();
System.out.println("异常通知生效。。。");
throw new RuntimeException(e);
}finally {
transactionManager.relese();
System.out.println("最终通知生效。。。");
}
return result;
}
});
}
}

配置spring的IOC:
<!--配置基于接口的动态代理对象,工厂方法创建-->
<bean id="serviceProxy" factory-bean="accountServiceFactory" factory-method="getProxy"></bean>

<bean id="accountServiceFactory" class="com.aiitec.proxy.AccountServiceFactory"></bean>

使用:
    @Autowired
@Qualifier("serviceProxy")
private IAccountService ccountService;



一:基于子类(第三方Cglib提供):
先导包
<!--cglib基于子类的动态代理-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
 
import com.aiitec.service.impl.AccountServiceCglib;
import com.aiitec.utils.TransactionManager;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

public class AccountServiceCglibFactory {
@Autowired
private AccountServiceCglib accountServiceCglib;//被代理对象,必须不是final类

@Autowired
private TransactionManager transactionManager;

/**
* 获取代理对象
* @return
*/
    public AccountServiceCglib getProxy() {
return (AccountServiceCglib) Enhancer.create(accountServiceCglib.getClass(), new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
Object result=null;
try{
transactionManager.beginTransaction();
System.out.println("cglib前置通知生效。。。");
result=method.invoke(accountServiceCglib,args);
transactionManager.commit();
System.out.println("cglib后置通知生效。。。");
}catch (Exception e){
transactionManager.rollback();
System.out.println("cglib异常通知生效。。。");
throw new RuntimeException(e);
}finally {
transactionManager.relese();
System.out.println("cglib最终通知生效。。。");
}
return result;
}
});
}
}

配置spring的IOC:
<!--配置基于子类的动态代理对象,工厂方法创建-->
<bean id="serviceCglib" factory-bean="accountCglibFactory" factory-method="getProxy"></bean>
<bean id="accountCglibFactory" class="com.aiitec.proxy.AccountServiceCglibFactory"></bean>


使用:
@Autowired
@Qualifier("serviceCglib")
private AccountServiceCglib serviceCglibProxy;
上一篇:java-Hibernate代理序列化并在客户端接收


下一篇:从静态代理,jdk动态代理到cglib动态代理-一文搞懂代理模式