动态代理和静态代理以及spring的aop

代理模式:为其他对象提供一种代理以控制对这个对象的访问。



代理角色和真是角色均实现同一个抽象角色,在代理角色内部有对真实角色的引用,从而用户可以通过对代理角色的访问来访问真是角色。


动态代理:被代理的对象可以在运行时动态的进行改变,增加了灵活性。

实现一个动态代理:

1.创建被代理的类以及接口。
public interface Subject
{
	public void request();
}
public class RealSubject implements Subject
{

	public void request()
	{
		System.out.println("realSubject");
		
	}
	
}


2。创建一个实现接口InvocationHandler的类,实现其invoke的方法。
public class DynamicProxy implements InvocationHandler
{
	private Object obj;
	
	public DynamicProxy(Object obj)
	{
		this.obj = obj;
	}
	
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable
	{	
		method.invoke(obj, args);	
		return null;
	}
}
在该类中有一个Object类型的成员变量,并且通过构造方法为其赋值,因为类型是Object,所以我们能够为任何类型的类做代理
查看java API帮助文档关于InvocationHandler接口的invoke()方法的说明:在代理实例上处理方法调用并返回结果。在与方法关联的代理实例上调用方法时,将在调用处理程序上调用此方法。即在第三步中执行subject.request()的时候会转到该方法中,并传入对应的方法以及参数


3。通过proxy的静态方法newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler handler)创建一个代理,然后我们就可以通过这个代理来对真是对象进行访问了
public class Client
{
	public static void main(String[] args)
	{
		RealSubject sub = new RealSubject();
		InvocationHandler handler = new DynamicProxy(sub);
		Class<?> classType = handler.getClass();
                //此处只能向上转型为Subject,而不能转型为RealSubject具体类型,具体类型是在invoke()方法中进行判断的
                Subject subject = (Subject)Proxy.newProxyInstance(classType.getClassLoader(), RealSubject.class.getInterfaces(), handler);
		subject.request();
		System.out.println(subject.getClass());
	}
}
subject.getClass()打印出来的结果是class $Proxy0,这是java动态生成的一个类,相当于代理角色。



对于spring的aop的应用和动态代理及其相似。
我们新建一个接口
public interface Person
{
	void info();
}
为该接口添加一个实现类
public class PersonImpl implements Person
{
	private String name;
	private String age;
	
	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getAge()
	{
		return age;
	}

	public void setAge(String age)
	{
		this.age = age;
	}

	public void info()
	{
		System.out.println("name = " + name + " " + "age = " + age);
	}
}

接下来我们创建拦截器,对方法的调用进行拦截

实现AfterReturningAdvice接口,实现其中的afterReturning()方法,该方法会在请求的方法调用之后执行
import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class MethodAfter implements AfterReturningAdvice
{
	public void afterReturning(Object arg0, Method arg1, Object[] arg2,
			Object arg3) throws Throwable
	{
		System.out.println("after method execute");
	}
}
下面方法实现了MethodBeforeAdvice方法,并实现其中的before方法,该方法会在请求的方法被调用之前执行
import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MethodBefore implements MethodBeforeAdvice
{

	public void before(Method method, Object[] args, Object target)
			throws Throwable
	{
		System.out.println("before call the method");
		System.out.println("methodName:" + method.getName() );
		System.out.println("args is:" + args);
		System.out.println("target is:" + target);
	}
}
然后我们配置spring的配置文件如下:
<bean id="personTarger" class="com.PersonImpl">
	<property name="name">
		<value>microbingbing</value>
	</property>
	<property name="age">
		<value>23</value>
	</property>
</bean>
<bean id="myAdvice" class="com.MethodBefore"></bean>
<bean id="methodAfter" class="com.MethodAfter"></bean>
<bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
	<property name="proxyInterfaces">
		<value>com.Person</value>
	</property>
	<property name="target">
		<ref local="personTarger"/>
	</property>
	<property name="interceptorNames">
		<list>
			<value>methodAfter</value>
			<value>myAdvice</value>
		</list>
	</property>
</bean>
新建一个测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class BeanTest
{
	public static void main(String[] args)
	{
		ApplicationContext context = new FileSystemXmlApplicationContext("applicationContext.xml");
		Person p = (Person)context.getBean("person");
		p.info();
	}
}
观察控制台输出结果:
before call the method
methodName:info
args is:[Ljava.lang.Object;@8acf6e
target is:com.PersonImpl@1112783
name = yangnianbing age = 24
after method execute



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值