码迷,mamicode.com
首页 > 其他好文 > 详细

17_AOP入门准备_Salay案例(利用动态代理)

时间:2016-06-14 06:23:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

【案例分析】

查看Salary:

  1.启动日志

  2.启动安全性的框架

  3.检查权限:如果有查看工资的权限,则查看工资,否则提示"权限不足"

 

【工厂截图】

技术分享

【SalaryManager.java】

package com.HigginCui.proxy.salary;

//目标接口
public interface SalaryManager {
    //查看工资
    public void showSalary();
}

【SalaryManagerImpl.java】

package com.HigginCui.proxy.salary;
//目标类
public class SalaryManagerImpl implements SalaryManager{
    @Override
    public void showSalary() {
        System.out.println("Show Salary...");
    }
}

【Logger.java】

package com.HigginCui.proxy.salary;
//日志类
public class Logger {
    public void logging(){
        System.out.println("logging ...");
    }
}

【Privilege.java】

package com.HigginCui.proxy.salary;
//权限类
public class Privilege {
    private String access;
    public String getAccess() {
        return access;
    }
    public void setAccess(String access) {
        this.access = access;
    }
}

【Security.java】

package com.HigginCui.proxy.salary;
//安全性框架类
public class Security {
    public void security(){
        System.out.println("security ...");
    }
}

【SalaryManagerInterceptor.java】

package com.HigginCui.proxy.salary;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
//拦截器
public class SalaryManagerInterceptor implements InvocationHandler{
    private Object target;
    private Logger logger;
    private Security security;
    private Privilege privilege;
    
    public SalaryManagerInterceptor(Object target,Logger logger,Security security,Privilege privilege){
        this.target=target;
        this.logger=logger;
        this.security=security;
        this.privilege=privilege;
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        //1.开启日志
        this.logger.logging();
        //2.开启安全性框架类
        this.security.security();
        //3.检查权限,如果是"admin",则允许查看,否则提示"权限不足"
        if(this.privilege.getAccess().equals("admin")){
            //4.调用目标方法
            method.invoke(target, args);
        }else{
            System.out.println("权限不足...");
        }
        return null;
    }

}

 

 【SalaryTest.java】

package com.HigginCui.test;

import java.lang.reflect.Proxy;

import org.junit.Test;

import com.HigginCui.proxy.salary.Logger;
import com.HigginCui.proxy.salary.Privilege;
import com.HigginCui.proxy.salary.SalaryManager;
import com.HigginCui.proxy.salary.SalaryManagerImpl;
import com.HigginCui.proxy.salary.SalaryManagerInterceptor;
import com.HigginCui.proxy.salary.Security;
//模拟客户端
public class SalaryTest {
    @Test
    public void testSalart(){
        Object target=new SalaryManagerImpl();
        Logger logger =new  Logger();
        Security security=new Security();
        Privilege privilege=new Privilege();
        /*
         * 如果是"admin",提示"Show Salary..."
         * 否则,提示"权限不足"
         */
        privilege.setAccess("admin");
        SalaryManagerInterceptor interceptor=new SalaryManagerInterceptor(target, logger, security, privilege);
        SalaryManager proxy=(SalaryManager) Proxy.newProxyInstance(
                                        target.getClass().getClassLoader(),
                                        target.getClass().getInterfaces(),
                                        interceptor);
        proxy.showSalary();
    }
}

【运行结果】

logging ...
security ...
Show Salary...

 

17_AOP入门准备_Salay案例(利用动态代理)

标签:

原文地址:http://www.cnblogs.com/HigginCui/p/5582617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!