面向切面编程(一)

进一步掌握动态代理

1.Role类

public class Role {
    private int id;
    private String roleName;
    private String note;
    public Role(int i, String string, String string2) {
        this.id=i;
        this.roleName=string;
        this.note=string2;
    }
    public Role() {
        
    }
    //setter getter
}
2.RoleService接口
public interface RoleService {
    
    public void printRole(Role role);
}
3.RoleService接口实现类
//我疑问过为什么加在实现类上而不是接口上,直到看到这个答案:
//@Service注解是标注在实现类上的,因为@Service是把spring容器中的bean进行实例化,也就是等同于new操作,只有实现类是可以进行new实例化的,而接口则不能,所以是加在实现类上的。
@Component
public class RoleServiceImpl implements RoleService {

    @Override
    public void printRole(Role role) {
        // TODO Auto-generated method stub
        System.out.println("{id="+role.getId()+",roleName="+role.getRoleName()+",note="+role.getNote()+"}");
    }

}
4.Interceptor接口
public interface Interceptor {
    public void before(Object obj);
    
    public void after(Object obj);
    
    public void afterReturning(Object obj);
    
    public void afterThrowing(Object obj);
}

5.角色拦截器RoleInterceptor

public class RoleInterceptor implements Interceptor {

    @Override
    public void before(Object obj) {
        System.out.println("准备打印角色信息");
    }

    @Override
    public void after(Object obj) {
        System.out.println("已经完成角色信息的打印处理");
    }

    @Override
    public void afterReturning(Object obj) {
        System.out.println("刚刚完成打印功能,一切正常。");
    }

    @Override
    public void afterThrowing(Object obj) {
        System.out.println("打印功能执行异常,查看角色是否为空?");
    }

}
6.使用动态代理实现流程
public class ProxyBeanUtil implements InvocationHandler {
    
    private Object obj;
    private Interceptor interceptor;
    /**
     * 获取动态代理对象
     * @param obj//被代理对象
     * @param interceptor
     * @return
     */
    public static Object getBean(Object obj,Interceptor interceptor) {
        ProxyBeanUtil _this=new ProxyBeanUtil();
        
        _this.obj=obj;
        
        _this.interceptor=interceptor;
        
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),_this);
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object retObj=null;
        //是否产生异常
        boolean exceptionFlag=false;
        interceptor.before(obj);
        try {
            //反射原有方法
            retObj=method.invoke(obj, args);
        } catch (Exception e) {
            exceptionFlag=true;
        }finally {
            interceptor.after(obj);
        }
        if(exceptionFlag) {
            interceptor.afterThrowing(obj);
        }else {
            interceptor.afterReturning(obj);
        }
        return retObj;
    }

}
7.ProxyBeanFactory的getBean方法
public class ProxyBeanFactory {
    public static <T> T getBean(T obj,Interceptor interceptor) {
        return (T) ProxyBeanUtil.getBean(obj,interceptor);
    }
}
8.测试
public class Test {
    public static void main(String[] args) {
        RoleService roleService=new RoleServiceImpl();
        Interceptor interceptor=new RoleInterceptor();
        RoleService proxy=ProxyBeanFactory.getBean(roleService, interceptor);
        Role role=new Role(1,"role_name_1","role_note_1");
        proxy.printRole(role);
        System.out.println("###### 测试afterthrowing方法#######");
        role=null;
        proxy.printRole(role);
    }
}
结果:
准备打印角色信息
{id=1,roleName=role_name_1,note=role_note_1
已经完成角色信息的打印处理
刚刚完成打印功能,一切正常。
###### 测试afterthrowing方法#######
准备打印角色信息
已经完成角色信息的打印处理
打印功能执行异常,查看角色是否为空?
原文地址:https://www.cnblogs.com/xc-xinxue/p/12376752.html