动态代理一
//---------------TargetInterface
package cn.ms.proxy;
public interface TargetInterface {
public void method1();
public String method2();
public int method3(int x);
}
//---------------Target
package cn.ms.proxy;
public class Target implements TargetInterface {
@Override
public void method1() {
System.out.println("method1 running...");
}
@Override
public String method2() {
System.out.println("method2 running...");
return "method2";
}
@Override
public int method3(int x) {
System.out.println("method3 running...");
return x;
}
}
//---------------ProxyTest
package cn.ms.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyTest {
public static void main(String[] args) {
Target target = new Target();
TargetInterface newProxy = (TargetInterface) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),//---需要增强类的接口(1)
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {//----proxy默认要增强的对象也就是(1)中传入的接口
System.out.println("目标方法前逻辑");
Object invoke = method.invoke(target, args);
System.out.println("目标方法后逻辑");
return invoke;
}
});
newProxy.method1();
System.out.println("------------------ method1 end ------------------");
String returnMethod2 = newProxy.method2();
System.out.println(returnMethod2);
System.out.println("------------------ method2 end ------------------");
int returnMethod3 = newProxy.method3(7);
System.out.println(returnMethod3);
System.out.println("------------------ method3 end ------------------");
}
}
动态代理二
//----------UserService
public interface UserService {
void save();
void delete();
void update();
void find();
}
//----------UserServiceImpl
public class UserServiceImpl implements UserService {
@Override
public void save() {
//开启事务
System.out.println("保存用户");
//关闭事务
}
@Override
public void delete() {
//开启事务
System.out.println("删除用户");
//关闭事务
}
@Override
public void update() {
//开启事务
System.out.println("更新用户");
//关闭事务
}
@Override
public void find() {
//开启事务
System.out.println("查找用户");
//关闭事务
}
}
//----------UserServiceProxyFactory
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class UserServiceProxyFactory implements InvocationHandler {
private UserService us;
public UserServiceProxyFactory(UserService us){
this.us = us;
}
public UserService getUserServiceProxy(){
UserService usProxy = (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),
us.getClass().getInterfaces(),
this);
return usProxy;
}
@Override
//--proxy当前的代理对象
//--method代理对象的方法
//--args方法的参数
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("打开事务");
Object obj = method.invoke(us, args);//这个method中的第一个参数是方法所在的对象
System.out.println("提交事务");
return obj;
}
}
//----------UserServiceProxyTest
import org.junit.Test;
public class UserServiceProxyTest {
@Test
public void fun(){
UserService us = new UserServiceImpl();
UserServiceProxyFactory factory = new UserServiceProxyFactory(us);
UserService usProxy = factory.getUserServiceProxy();
usProxy.save();
}
}
动态代理使用(处理全局乱码的问题)
package com.ithiema.web.filter;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class EncodingProxyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletRequest enhanceRequest = (HttpServletRequest) Proxy.newProxyInstance(
httpServletRequest.getClass().getClassLoader(),
httpServletRequest.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if(method.getName().equals("getParameter")){
String invoke = (String) method.invoke(httpServletRequest, args);
invoke = new String(invoke.getBytes("iso-8859-1"), "UTF-8");
return invoke;
}
return method.invoke(httpServletRequest, args);
}
});
chain.doFilter(enhanceRequest, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}