In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "Java how to achieve a simple interceptor operation through dynamic agents". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "Java how to achieve a simple interceptor operation through dynamic agents" can help you solve your doubts.
I. Agency
Before using dynamic proxies to implement interceptors, let's take a brief look at what Java proxies are.
A proxy, as its name implies, does not directly manipulate the proxied object (which is called by the target object below, which sounds more comfortable), but indirectly uses the methods in the target object through a proxy object. Agents are divided into two modes, one is static agent, the other is dynamic agent. Next, let's write an example of a static agent.
Whether it is a static proxy or a dynamic proxy, the target object (target) implements an interface (interface). Note that if you use the proxy provided by cglib, you do not have to implement the interface, but through subclasses, which will not be discussed for the time being.
(1) define an interface public interface IUserDao {void save ();} (2) define the target object (target) public class UserDaoImpl implements IUserDao {public void save () {System.out.println ("- saved data -");}} (3) define the proxy object public class UserDaoProxy implements IUserDao {private IUserDao target / / place the target object in the proxy object public UserDaoProxy (IUserDao target) {this.target = target;} public void save () {System.out.println ("- start transaction -"); target.save (); System.out.println ("- commit transaction -");}}
Test it:
Public class Test {public static void main (String [] args) {IUserDao userDao = new UserDaoImpl (); UserDaoProxy proxy = new UserDaoProxy (userDao); proxy.save (); / / call save method through proxy object}}
The output is as follows:
-start the transaction-
-data has been saved-
-commit transaction-
The problem with this approach is that the proxy object must also implement the same interface implemented by the proxy object, which leads to serious coupling. So, let's use an improved approach, that is, dynamic proxy (jdk proxy).
Dynamic proxy also requires the target object (target) to implement an interface.
(1) define an interface (IUserDao)
(2) define a target object class (UserDaoImpl)
(3) create a dynamic proxy class
Public class ProxyFactory {/ / maintains a target object private Object target; public ProxyFactory (Object target) {this.target = target;} / / generates a proxy object public Object getProxyInstance () {System.out.println ("- target class---" + target.getClass ()) for the target object. System.out.println ("- target interfaces---" + target.getClass () .getInterfaces ()) Return Proxy.newProxyInstance (target.getClass (). GetClassLoader (), target.getClass (). GetInterfaces (), new InvocationHandler () {public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System.out.println ("- start transaction 2 -") / / execute the target object method Object returnValue = method.invoke (target, args); System.out.println ("- commit transaction 2Murmuri -"); return returnValue;}});}}
Test it:
Public class Test {public static void main (String [] args) {/ / Target object IUserDao target = new UserDao (); System.out.println (target.getClass ()); / / create proxy object for target object IUserDao proxy = (IUserDao) new ProxyFactory (target). GetProxyInstance (); System.out.println ("- proxy----:" + proxy.getClass ()) Proxy.save (); proxy.delete ();}}
Output result:
Class com.jd.pattern.proxy.dynamicProxy.UserDao
-target class---class com.jd.pattern.proxy.dynamicProxy.UserDao
-target interfaces--- [Ljava.lang.Class;@1fb3ebeb]
-proxy----:class com.sun.proxy.$Proxy0
-start transaction 2-
-Save completed-
-commit transaction 2Murray-
-start transaction 2-
-deletion completed-
-commit transaction 2Murray-
Second, use dynamic agents to implement a simple interceptor
Since dynamic proxy is adopted, there must be interfaces, target classes, proxy classes, and an interceptor.
1. Define an interface public interface BusinessFacade {void doSomething ();} 2, define a target object public class BusinessClass implements BusinessFacade {public void doSomething () {System.out.println ("call doSomething method in business component BusinessClass");}} 3, create interceptor public class InterceptorClass {public void before () {System.out.println ("call method in InterceptorClass: before ()") } public void after () {System.out.println ("call method in InterceptorClass: after ()");}} 4. Create proxy public class DynamicProxyHandler {/ / declare proxied object private Object target; / / create interceptor InterceptorClass interceptor = new InterceptorClass () / / dynamically generate a proxy object and bind the proxy class to the proxy processor public Object getProxyInstance (final Object target) {this.target = target Return Proxy.newProxyInstance (target.getClass (). GetClassLoader (), target.getClass (). GetInterfaces (), new InvocationHandler () {public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {interceptor.before (); Object result = method.invoke (target, args) Interceptor.after (); return result;});}}
Test it:
Public class Test {public static void main (String [] args) {/ / create dynamic proxy tool DynamicProxyHandler proxyHandler = new DynamicProxyHandler (); / / create business component BusinessFacade target = new BusinessClass (); / / get proxy object BusinessFacade proxy = (BusinessFacade) proxyHandler.getProxyInstance (target); / / call target object method proxy.doSomething () through proxy object;}}
Output result:
Call the method in InterceptorClass: before ()
Call the doSomething method in the business component BusinessClass
Call the method in InterceptorClass: after ()
Read this, the "Java how to achieve a simple interceptor operation through dynamic agents" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.