In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use jdk dynamic agent and Cglib dynamic agent in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
JDK dynamic agent
Jdk dynamic proxy can only proxy interface, because java's single inheritance dynamic proxy class inherits Proxy class, so it can not inherit other classes, so it can only rely on interface to implement the interface of the proxied class.
Case study
Public interface UserManager {
Void addUser (Integer id,String userName)
Void delUser (Integer id)
Void findUser (Integer id)
Void modifyUser (Integer id,String userName)
}
Public class UserManagerImpl implements UserManager {
@ Override
Public void addUser (Integer id,String userName) {
System.out.println ("userManager.addUser ()")
}
@ Override
Public void delUser (Integer id) {
System.out.println ("userManager.delUser ()")
}
@ Override
Public void findUser (Integer id) {
System.out.println ("userManager.findUser ()")
}
@ Override
Public void modifyUser (Integer id,String userName) {
System.out.println ("userManager.modifyUser ()")
}
}
Public class LogHandler implements InvocationHandler {
/ / Target object
Private Object targetObject
Public Object newProxyInstance (Object targetObject) {
This.targetObject=targetObject
/ *
* param1 the classloader to load the proxy object
* APIs that need to be implemented by param2
* when the param3 dynamic proxy method is executed, the invoke method is called to execute
* /
Return Proxy.newProxyInstance (targetObject.getClass (). GetClassLoader (), targetObject.getClass (). GetInterfaces (), this)
}
@ Override
Public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {
/ / print target method parameters
For (int I = 0; I < args.length; iTunes +) {
System.out.println (args [I])
}
Object ret=null
Try {
System.out.println ("start"); / / before executing the target method
Ret=method.invoke (targetObject, args)
System.out.println ("success"); / / after executing the target method
} catch (Exception e) {
E.printStackTrace ()
System.out.println ("error")
Throw e
}
Return ret
}
}
Cglib dynamic agent
Cglib means that a subclass inherits the specified class and overrides the class method, so it is best not to add a final modifier to the specified class
Case study
Cglib needs to rely on packages
Cglib
Cglib
2.2.2
/ / Cglib dynamic proxy implements MethodInterceptor interface
Public class CglibProxy implements MethodInterceptor {
Private Object target
@ Override Zhengzhou abortion specialist Hospital http://www.03912316666.com/
Public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {
System.out.println ("start")
/ / output target method parameters
For (int I = 0; I < args.length; iTunes +) {
System.out.println (args [I])
}
Object invoke=method.invoke (target, args)
System.out.println ("end")
Return invoke
}
Public Object getCglibProxy (Object targetObject) {
This.target=targetObject
Enhancer enhancer=new Enhancer ()
/ / because Cglig needs to generate a subclass, you need to specify a parent class
Enhancer.setSuperclass (targetObject.getClass ())
Enhancer.setCallback (this)
Object result=enhancer.create (); / / create and return a proxy object
Return result
}
Public static void main (String [] args) {
CglibProxy cglibProxy=new CglibProxy ()
UserManagerImpl userManager= (UserManagerImpl) cglibProxy.getCglibProxy (new UserManagerImpl ())
UserManager.addUser (66 "6666")
}
}
/ / the parent interface is not implemented
Public class UserManagerImpl {
Public void addUser (Integer id,String userName) {
System.out.println ("userManager.addUser ()")
}
Public void delUser (Integer id) {
System.out.println ("userManager.delUser ()")
}
Public void findUser (Integer id) {
System.out.println ("userManager.findUser ()")
}
Public void modifyUser (Integer id,String userName) {
System.out.println ("userManager.modifyUser ()")
}
}
Thank you for reading! This is the end of this article on "how to use jdk dynamic Agent and Cglib dynamic Agent in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.