Use REFLECT Proxy

xiaoxiao2021-03-06  75

Use Reflect Proxy to post your business method and put it in other places.

Concept: 1.Proxy instance agent instance 2.InvocationHandler call processor to write business objects and interface: public interface businessinterface {public void processbusiness ();

Public Class BusinessObject Implements BusinessInterface {PUBLIC VOID ProcessBusiness () {System.out.Println ("this is the business code!");}};

Write a calling processor, the business method is called here:

Import java.lang.reflect.invocationhandler; import java.lang.reflect.method; import java.lang.reflect.InvocationTargeTexception;

public class Myhandler implements InvocationHandler {Object businessObject; Myhandler (Object delegate) {businessObject = delegate;} public Object invoke (Object proxy, Method method, Object [] args) throws IllegalAccessException, InvocationTargetException {Object o = null; System.out.println ("Log: Start Execute Business"); // You can use log4j, then record logs are separated from // if (ISPERMISSION (User) {// can consider using safety control //Transaction.begin (); // can consider transaction control // long begin = system.currenttimemillis (); o = method.invoke (businessObject, args); // Execute business method // long end = system.currentimemillis () ; //System.out.println (">>log:" (end - begin)); // Performance statistics, //transaction.commit();///} system.out.println ("log : End Execute Business "); Return O;

Construct the agent instance and use the proxy instance to perform business methods import java.lang.reflect.invocationhandler; import java.lang.reflect.Proxy;

转载请注明原文地址:https://www.9cbs.com/read-97880.html

New Post(0)