`
bluesky_4
  • 浏览: 7001 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

有关代理模式的例子

阅读更多
1.静态代理

首先定义接口
package staticProxy;

public interface IHello {
	public void hello(String name);
}


然后定义业务逻辑实现类
package staticProxy;

public class HelloSpeaker implements IHello {

	@Override
	public void hello(String name) {
		// TODO Auto-generated method stub
		System.out.println("Hello, "+name);
	}

}


最后定义代理类,同样实现之前定义的接口

package staticProxy;

import java.util.logging.Logger;
import java.util.logging.Level;;

public class HelloProxy implements IHello {

	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	private IHello helloObject;
	
	public HelloProxy(IHello helloObject){
		this.helloObject = helloObject;
	}
	
	
	@Override
	public void hello(String name) {
		// TODO Auto-generated method stub
		log("hello method starts......");
		
		helloObject.hello(name);
		
		log("hello method ends......");
		
	}

	private void log(String msg){
		logger.log(Level.INFO,msg);
	}
}


测试类

package staticProxy;

public class ProxyDemo {
	public static void main(String[] args){
		IHello  proxy = new HelloProxy(new HelloSpeaker());
		proxy.hello("Cuiliang");
	}
}




动态代理

编写动态代理类
package DynamicProxy;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.lang.reflect.*;

public class LogHandler implements InvocationHandler{
	
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	private Object delegate;
	
	public Object bind(Object delegate){
		this.delegate = delegate;
		return Proxy.newProxyInstance(
				delegate.getClass().getClassLoader(), 
				delegate.getClass().getInterfaces(), 
				this);
	}
	
	public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
		
		Object result = null;
		
		try{
			log("method starts......" + method);
			
			result = method.invoke(delegate, args);
			
			log("method ends......" + method);
			
		}
		catch(Exception e){
			log(e.toString());
		}
		
		return result;
	}
	private void log(String msg){
		logger.log(Level.INFO,msg);
	}
}


测试类


package DynamicProxy;

import staticProxy.HelloSpeaker;
import staticProxy.IHello;

public class ProxyDemo {
	public static void main(String[] args){
		LogHandler logHandler = new LogHandler();
		
		IHello helloProxy =
			(IHello)logHandler.bind(new HelloSpeaker());
		helloProxy.hello("Cuiliang");
	}
}



Spring AOP 实现

package AOP;

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.aop.MethodBeforeAdvice;

public class LogBeforeAdvice implements MethodBeforeAdvice{
	
	private Logger logger = Logger.getLogger(this.getClass().getName());
	
	public void before(Method method, Object[] args, 
			   Object target) throws Throwable{
		logger.log(Level.INFO,"method starts..." + method);
	}
}



<?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:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<bean id="logBeforeAdvice"
		  class="AOP.LogBeforeAdvice"/>
	<bean id="helloSpeaker"
		  class="staticProxy.HelloSpeaker"/>
		  
	<bean id="helloProxy"
		  class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="proxyInterfaces"
				  value="staticProxy.IHello"></property>
		<property name="target" ref="helloSpeaker"></property>
		<property name="interceptorNames">
			<list>
				<value>logBeforeAdvice</value>
			</list>
		</property>
	</bean>
</beans>


测试类
package AOP;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import staticProxy.IHello;

public class SpringAOPDemo {
	public static void main(String[] args){
		ApplicationContext context = 
					new ClassPathXmlApplicationContext("AOP/bean-config.xml");
		IHello helloProxy = 
				(IHello)context.getBean("helloProxy");
		helloProxy.hello("Cuiliang");
	}
}


需要 spring-aop.jar 包,而spring-aop.jar包又依赖,spring.jar和commons-logging-1.0.4.jar两个包。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics