first, why to use dynamic proxy
when an object or multiple objects implement the N method, there are three ways to add the object and the N methods of multiple objects to a common method, such as adding all the methods of all objects to the transaction at this time:
method One: An object one method to add, it is obvious that this method is a relatively stupid method.
method Two: Add a static proxy object to this static proxy object implementation to add the interface of the transaction object. Then add a transaction to each method inside the static proxy object.
Method Three: Use dynamic proxy objects for dynamic load transactions.
the use of dynamic proxies is to allow the object to implement the open and close principle, opening to the extension, and closing the modification. Spring's AOP is actually implemented using the dynamic proxy +IOC container.
Second, the code implementation.
Usermanger.java interface
Package Com.tgb.spring;public interface Usermanager {public void AddUser (String username,string password);p ublic void Deluser (int userid);p ublic String finduserbyid (int userid);p ublic void modifyuser (int userid,string username,string password);}
Method One of the usual practices:
Usermangerimpl.java
Package Com.tgb.spring;public class Usermanagerimpl implements Usermanager {public void AddUser (string username, string p Assword) {checksecurity (); System.out.println ("Usermanager.adduser");} public void Deluser (int userId) {checksecurity (); System.out.println ("Usermanager.deluser");} Public String Finduserbyid (int userId) {checksecurity (); System.out.println ("Usermanager.finduserbyid"); return "Zhang San";} public void ModifyUser (int userId, string Username, string password) {checksecurity (); System.out.println ("Usermanager.modifyuser");} private void CheckSecurity () {System.out.println ("checksecurity");}}
Client.java
Package Com.tgb.spring;public class Client {/** * @param args */public static void main (string[] args) {Usermanager Userma Nager=new Usermanagerimpl (); Usermanager.adduser ("11", "1231");}}
Method two static proxies:
Usermanagerimpl.java
Package Com.tgb.spring;public class Usermanagerimpl implements Usermanager {public void AddUser (string username, string p Assword) {//checksecurity (); System.out.println ("Usermanager.adduser");} public void Deluser (int userId) {//checksecurity (); System.out.println ("Usermanager.deluser");} Public String Finduserbyid (int userId) {//checksecurity (); System.out.println ("Usermanager.finduserbyid"); return "Zhang San";} public void ModifyUser (int userId, string Username, string password) {//checksecurity (); System.out.println ("Usermanager.modifyuser");} private void CheckSecurity () {//system.out.println ("checksecurity");////}}
Usermanagerimplproxy.java
Package Com.tgb.spring;public class Usermanagerimplproxy implements Usermanager {private Usermanager usermanager; Public Usermanagerimplproxy (Usermanager usermanager) {This.usermanager=usermanager;} public void AddUser (string username, string password) {//TODO auto-generated method Stubchecksecurity (); Usermanager.adduser (username, password);} public void Deluser (int userId) {//TODO auto-generated method Stubchecksecurity (); Usermanager.deluser (userId);} Public String Finduserbyid (int userId) {//TODO auto-generated method Stubchecksecurity (); return Usermanager.finduserbyid (userId);} public void ModifyUser (int userId, string Username, string password) {//TODO auto-generated method Stubchecksecurity (); US Ermanager.modifyuser (userId, username, password);} private void CheckSecurity () {System.out.println ("checksecurity");}}
Client.java
Package Com.tgb.spring;public class Client {/** * @param args */public static void main (string[] args) {//TODO Auto-gener Ated method Stubusermanagerimpl uesrmangerimpl=new Usermanagerimpl (); Usermanager usermanager=new Usermanagerimplproxy (Uesrmangerimpl); Usermanager.adduser ("11", "1231");}}
Method Three: Dynamic agent
Usermanagerimpl.java the same as the Usermanagerimpl.java of method two to remove the Usermanagerimplproxy.java
Add a new class:
Securityhandler.java
Package Com.tgb.spring;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;public class Securityhandler implements Invocationhandler {private Object Targetobject;public Object Createproxyinstance (Object targetObject) {This.targetobject=targetobject;return proxy.newproxyinstance ( Targetobject.getclass (). getClassLoader (), Targetobject.getclass (). Getinterfaces (), this);} public object invoke (object proxy, Method method, object[] args) throws Throwable {//TODO auto-generated Method stubchecks Ecurity ();//Call the Target method object Ret=method.invoke (TargetObject, args); return ret;} private void CheckSecurity () {System.out.println ("checksecurity");}}
Client.java
Package Com.tgb.spring;public class Client {public static void main (string[] args) {Securityhandler handler=new securityh Andler (); Usermanager usermanager= (Usermanager) handler.createproxyinstance (New Usermanagerimpl ()); UserManager.addUser ("Zha "," 123 ");}}
The effect of three methods is the same
Third, summary.
The use of dynamic agents has brought great convenience to our coding, which solves the scheme of dynamically serving objects. The combination of dynamic Agent +IOC container allows us to provide services to the system more convenient, so we also realize the open and close principle. This also enables the object to be withdrawn when it is not needed and to serve the developer wholeheartedly.