码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-12-07 14:37:05      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:异常   oca   manager   voc   eth   实现   cer   count   group   

一:基于接口(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;

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

标签:异常   oca   manager   voc   eth   实现   cer   count   group   

原文地址:https://www.cnblogs.com/zou-rong/p/12001345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!