17_AOP Getting Started _salay case (leveraging dynamic proxies)

Source: Internet
Author: User

"Case Study"

View Salary:

1. Start the log

2. Start the security framework

3. Check permissions: If you have permission to view wages, see pay, otherwise prompt "Insufficient permissions"

Factory

"Salarymanager.java"

 Package com. HigginCui.proxy.salary; // Target Interface  Public Interface Salarymanager {    // View Payroll public    void  showsalary ();}

"Salarymanagerimpl.java"

 Package com. HigginCui.proxy.salary; // Target Class  Public class Implements salarymanager{    @Override    publicvoid  showsalary () {        System.out.println ("Show Salary ...");}    }

"Logger.java"

 Package com. HigginCui.proxy.salary; // Log Class  Public class Logger {    publicvoid  logging () {        System.out.println ("Logging ...") );    }}

"Privilege.java"

 Package com. HigginCui.proxy.salary; // Permission Class  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; // Security Framework Classes  Public class Security {    publicvoid  Security () {        System.out.println ("Security ... ");    }}

"Salarymanagerinterceptor.java"

 Packagecom. HigginCui.proxy.salary;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;//Interception Device Public classSalarymanagerinterceptorImplementsinvocationhandler{PrivateObject Target; PrivateLogger Logger; Privatesecurity security; PrivatePrivilege Privilege;  Publicsalarymanagerinterceptor (Object target,logger logger,security security,privilege Privilege) { This. target=Target;  This. logger=logger;  This. security=security;  This. privilege=privilege; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//1. Turn on the log         This. logger.logging (); //2. Turn on the security framework class         This. security.security (); //3. Check permissions, if "admin", allow view, otherwise prompt "Insufficient permissions"        if( This. privilege.getaccess (). Equals ("admin")){            //4. Calling the target methodMethod.invoke (target, args); }Else{System.out.println ("Insufficient permissions ..."); }        return NULL; }}

"Salarytest.java"

 Packagecom. Higgincui.test;ImportJava.lang.reflect.Proxy;Importorg.junit.Test;Importcom. HigginCui.proxy.salary.Logger;Importcom. HigginCui.proxy.salary.Privilege;Importcom. HigginCui.proxy.salary.SalaryManager;Importcom. HigginCui.proxy.salary.SalaryManagerImpl;Importcom. HigginCui.proxy.salary.SalaryManagerInterceptor;Importcom. HigginCui.proxy.salary.Security;//Impersonate a client Public classsalarytest {@Test Public voidTestsalart () {Object target=NewSalarymanagerimpl (); Logger Logger=NewLogger (); Security Security=NewSecurity (); Privilege Privilege=NewPrivilege (); /** If "admin", Prompt "Show Salary ..." * Otherwise, prompt "Insufficient permissions"*/privilege.setaccess ("Admin"); Salarymanagerinterceptor Interceptor=NewSalarymanagerinterceptor (target, logger, security, privilege); Salarymanager Proxy=(Salarymanager) proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), Interceptor)        ;    Proxy.showsalary (); }}

"Run Results"

Logging ... security ... Show Salary ...

17_AOP Getting Started _salay case (leveraging dynamic proxies)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.