Spring (10) implements AOP technology through dynamic proxies (JDK proxies) and Cglib

Source: Internet
Author: User
Tags assert throwable

All interface methods for the target object

Package Test.spring.service;public interface Personservice {public String getpersonname (Integer id);p ublic void Save ( String name);}

Target Object

Package Test.spring.service.impl;import test.spring.service.personservice;//proxy object Implementation target object all interfaces public class Personservicebean implements Personservice {private String user = Null;public Personservicebean () {}public Personservicebean (String user) {super (); this.user = user;} Public String GetUser () {return user;} public void SetUser (String user) {this.user = user;} @Overridepublic String getpersonname (Integer ID) {//TODO auto-generated method Stubreturn "Getpersonname";} @Overridepublic void Save (String name) {//TODO auto-generated method StubSystem.out.println ("Save ()->>" + name);}}

The

proxy object, which intercepts all business methods, determines whether the user has permissions based on whether or not it is null, allows execution of a business method with permission, and does not execute without permission.

package test.spring.aop;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;import Test.spring.service.impl.personservicebean;public Class Proxyfactory implements Invocationhandler {Private object proxyobject;//Create proxy object instance public object Createproxyintance (Object proxyobject) {this.proxyobject = proxyobject;/* * First parameter: Class loader * Second parameter: Gets the interface of the target object, and proxy will implement all of these interfaces * The third parameter: A callback function that determines which class of interface is implemented */return Pro Xy.newproxyinstance (This.proxyObject.getClass (). getClassLoader (), This.proxyObject.getClass (). Getinterfaces (), this);} /* First parameter: Proxy object * Second parameter: Blocked method * Third parameter: Method input parameter */@Overridepublic object Invoke (Object proxy, method, object[] Ar GS) throws Throwable {Personservicebean Personservicebean = (personservicebean) this.proxyobject;object target = null;// User is not NULL description has permission if (Personservicebean.getuser ()! = NULL) {//Delegate call to proxy object method to target object = Method.invoke (Proxyobject, args);} return target;}} 

Permission test

Package Test.spring.junit;import static Org.junit.assert.*;import Org.junit.test;import Test.spring.aop.proxyfactory;import Test.spring.service.personservice;import Test.spring.service.impl.personservicebean;public class Aoptest {@Testpublic void Test () {Proxyfactory pfactory = new Proxyfactory ();//Personservice pservice= (Personservice)//Pfactory.createproxyintance (New Personservicebean ("KKK" ), or//change to the default function Pservice.save ("PPP") will not execute, because the preceding user=null, equivalent to no permission to execute personservice pservice = (personservice) Pfactory.createproxyintance (New Personservicebean ());p Service.save ("PPP");}


The above applies to the target object implementation of the interface, if the target object does not have an interface, will choose to use Cglib

First Import Cglib-nodep-2.1_3.jar

Package test.spring.service.impl;//proxy object implements target object all interfaces public class PersonServiceBean2 {private String user = Null;public PersonServiceBean2 () {}public PersonServiceBean2 (String user) {super (); this.user = user;} Public String GetUser () {return user;} public void SetUser (String user) {this.user = user;} public void Save (String name) {//TODO auto-generated method StubSystem.out.println ("Save ()->>" + name);}}

Package Test.spring.aop;import Java.lang.reflect.method;import Test.spring.service.impl.personservicebean2;import Net.sf.cglib.proxy.callback;import Net.sf.cglib.proxy.enhancer;import Net.sf.cglib.proxy.MethodInterceptor; Import net.sf.cglib.proxy.methodproxy;//target object does not have interface public class Cglibproxyfactory implements Methodinterceptor {private Object proxyobject;//Create proxy object instance public object Createproxyintance (object proxyobject) {this.proxyobject = Proxyobject; Enhancer enhancer = new enhancer ();/* need to set the parent class, which is the target class. The object to be created inherits the target class, overwriting all non-final methods in the target class, and then adding some of its own code */enhancer.setsuperclass (This.proxyObject.getClass ()) in the overridden code; Enhancer.setcallback ((Callback) this); return Enhancer.create ();} /* First parameter: The proxy object itself * The second parameter: the method being intercepted * The third parameter: The parameter of the method * The fourth parameter: the proxy object of the method *///the business method of the contemporary objects call this method when the callback is @overridepublic object inte Rcept (Object Proxy, method, object[] Args,methodproxy methodproxy) throws Throwable {PersonServiceBean2 Personservicebean = (PersonServiceBean2) this.proxyobject;object target = null;//useR is not NULL description has permission if (Personservicebean.getuser ()! = NULL) {//Delegate call to method to target object = Methodproxy.invoke (Proxyobject, args);} return target;}}

Package Test.spring.junit;import static Org.junit.assert.*;import Org.junit.test;import Test.spring.aop.cglibproxyfactory;import Test.spring.aop.proxyfactory;import Test.spring.service.PersonService; Import Test.spring.service.impl.personservicebean;import Test.spring.service.impl.personservicebean2;public class AOPTest2 {@Testpublic void Test () {cglibproxyfactory pfactory = new Cglibproxyfactory ();// At this point Personservicebean has no interface PersonServiceBean2 Pservice = (PersonServiceBean2) pfactory.createproxyintance (new PersonServiceBean2 ("KKK"));p Service.save ("PPP");}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you want to reprint, please specify the source: Http://blog.csdn.net/lindonglian

Spring (10) implements AOP technology through dynamic proxies (JDK proxies) and Cglib

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.