Small Example of JDK dynamic proxy

Source: Internet
Author: User

1. File List:

Business interface: userservice

Business implementation: userserviceimpl

Proxy class call handler implementation: proxyhandler

2. Do not talk nonsense. paste the Code:

package com.niewj.service;import com.niewj.model.User;public interface UserService {void add(User user);void delete(User user);}
package com.niewj.service;import com.niewj.model.User;public class UserServiceImpl implements UserService {@Overridepublic void add(User user) {System.out.println("User Saved. & ");}@Overridepublic void delete(User user) {System.out.println("User Deleted. &");}}

Package COM. niewj. service; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method;/*** JDK dynamic proxy simulation ** 1. first, identify what is the target object and what is the proxy object proxy !! ** 2. Each proxy object has a related invocationhandler object. * When a contemporary rational object is generated, it is the created proxy object. * The invocationhandler object is used to automatically call the invoke method implemented in the handler class. * The following passage comes from @ javadoc @ * <p> Each proxy instance has an associated invocation handler. * When a method is invoked on a proxy instance, the method * invocation is encoded and dispatched to the <code> invoke </code> * method of its invocation handler. ** 3. another reason is that I found that the TMD output sequence in my eclipse console is incorrect. I often thought that something went wrong with my character. * I don't know if the console information is not stack-based output. (Good detour) **/public class proxyhandler implements invocationhandler {// create a proxy object for this target class. Private object target; // The instance that transmits the proxy target because the proxy processor needs it. You can also use the set method. Public proxyhandler (object target) implements this.tar get = target;}/** this method is called for the proxy object. * Note that the object called by the internal method is the target object. Do not write an error. * // @ Overridepublic object invoke (Object proxy, method, object [] ARGs) throws throwable {object ret = NULL; // 1. cutdomainbefore (method. getname (); ret = method. invoke (target, argS); // 2. cutaskafter (method. getname (); return ret;} public void cutaskbefore (string mname) {system. err. println ("calling ____" + mname + "() ____ method");} public void cutpattern after (string mname) {system. err. println ("calling ____" + mname + "() ____ method ");}}

Testing Area:

Package COM. niewj; import Java. lang. reflect. proxy; import Org. JUnit. test; import COM. niewj. model. user; import COM. niewj. service. proxyhandler; import COM. niewj. service. userservice; import COM. niewj. service. userserviceimpl; public class springproxytest {@ test @ suppresswarnings ("rawtypes") Public void testjdkdynamicproxy () {/* 1. obtain the userserviceimpl object -- the target object -- that is, the object to be proxy. */Userservice = new userserviceimpl (); // obtain the current class name class clazz = userservice. getclass ();/** 2. Obtain the proxy object. * Each proxy object has a related invocationhandler object. The invoke of this handler object is used to implement the custom tricky behavior to be completed in the call *. You can use the proxy class and custom call processing logic to generate a proxy object. */Userservice userserviceproxy = (userservice) proxy. newproxyinstance (clazz. getclassloader (), clazz. getinterfaces (), new proxyhandler (userservice); userserviceproxy. add (new user ("dotjar"); userserviceproxy. delete (new user ("DDD "));}}

3. Conclusion: in the code, look up

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.