spring随笔(二) AOP(二)

2014-11-24 02:22:06 · 作者: · 浏览: 1
.update(customer); }

打印结果:

session.beginTranscation()
CustomerServiceImpl......save...张珊
session.getTranscation().commit()
-------------------------------
session.beginTranscation()
CustomerServiceImpl......update...张珊
session.getTranscation().commit()

2,使用CGLIB代理

在使用Spring的CGLIB代理过程中主要使用到的类为:net.sf.cglib.proxy.Enhancer。其核心方法为:Object hancer.create();

代码如下:

MyCallbackFactory类,需要注意的是 该类实现的接口和JDK实现的接口名字完全一样,接口结构也一致,单spring对其做了另外的调整。所以开发者们不能实现错接口。

package com.xiaohui.proxy;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;
public class MyCallbackFactory implements  InvocationHandler {
	private Object target;
	public Object getInstance(Object target){
		this.target = target;
		Enhancer hancer = new Enhancer(); 
		hancer.setClassLoader(target.getClass().getClassLoader());
		hancer.setSuperclass(target.getClass());
		hancer.setCallback(this);
		return hancer.create();
	}
	@Override
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		System.out.println("your before logic progream.....");
		Object obj =  method.invoke(target, args);
		System.out.println("your after logic progream.....");
		return obj;
	}
}

测试类:

@Test
	public void testProxy2() throws Exception {
		CustomerServiceImpl service = new CustomerServiceImpl();
		Customer customer = new Customer();
		customer.setName("张珊");
		MyCallbackFactory factory = new MyCallbackFactory();
		CustomerServiceImpl proxy =  (CustomerServiceImpl) factory.getInstance(service);
		proxy.update(customer);
		System.out.println(proxy.getClass().getSuperclass().getName());
	}

打印结果:

your before logic progream.....
CustomerServiceImpl......update...张珊
your after logic progream.....
com.xiaohui.proxy.CustomerServiceImpl
由此可以看出该代理对象为真实对象的子类。需要代理的对象实不实现接口都无所谓,都可以被CGLIB代理。

3,通过实现org.aopalliance.intercept.MethodInterceptor接口,使用spring的org.springframework.aop.framework.ProxyFactoryBean类配置xml获取代理,内部机制就是上面的两种。

TranscationInterceptor类:

package com.xiaohui.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class TranscationInterceptor implements MethodInterceptor {
	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		System.out.println("your Transcation...logic progream...Before....");
		Object obj = invocation.proceed();
		System.out.println("your Transcation...logic progream...After....");
		return obj;
	}
}

ICustomeService接口:

package com.xiaohui.aop;
public interface ICustomeService {
	void save(Customer c);
	void update(Customer c);
}

CustomerServiceImpl类:

package com.xiaohui.aop;
public class CustomerServiceImpl implements ICustomeService{
	public void save(Customer customer) {
		System.out.println("CustomerServiceImpl......save..."+customer.getName());
	}
	public void update(Customer c) {
		System.out.println("CustomerServiceImpl......update..."+c.getName());
	}
}

applicationContext.xml:

    

    

	
     
	
      
       
       
        
       
       
        
        
         transcationInteceptor
         
        
       
     

    

使用spring的这种配置,被代理的类如果没有实现接口就不用在ProxyFactoryBean中配 ,这样没有接口的代理会通过CGLIB创建代理。有接口的会通过JDK动态代理来创建。
测试代码:

@Test
	public void testProx