Cglib dynamic production principle

Source: Internet
Author: User

The main function is to intercept javaBean methods. First, create an interception class to implement the MethodInterceptor interface. [Java]/*****/package org. interceptor; import java. lang. reflect. method; import net. sf. cglib. proxy. enhancer; import net. sf. cglib. proxy. methodInterceptor; import net. sf. cglib. proxy. methodProxy;/*** created by: HF ** creation date: Jan 15,201 3 * file name: org.interceptor.org. interceptor * @ version 1.1 * -------------- Change Record description ---------------------- * modification time: * modification content: * Reason: * modifier: * modified version: */public class LazyInitializer I Mplements MethodInterceptor {public static Object createBean (Class <?> Clazz) {Enhancer enhancer = new Enhancer (); enhancer. setSuperclass (clazz); enhancer. setCallback (new LazyInitializer (); return enhancer. create () ;}@ Override public Object intercept (Object obj, Method method, Object [] args, MethodProxy proxy) throws Throwable {return proxy. invokeSuper (obj, args) ;}} you can create a javabean class: [java] package com. entity; import com. enums. sex; public class User {<span style = "white-space: pre"> </span>/** <span style = "white-space: pre "> </span> * <span style =" white-space: pre "> </span> */<span style =" white-space: pre "> </span> private static final long serialVersionUID =-5712249899395165324L; <span style =" white-space: pre "> </span> private String name; <span style = "white-space: pre"> </span> <span style = "white-space: pre"> </span> private Sex sex; <span style = "white-space: pre"> </span> <span style = "white-space: pre"> </span> private Double weight; <span style = "white-space: pre"> </span> <span style = "white-space: pre "> </span> <span style =" white-space: pre "> </span> public Double getWeight () {<span style =" white-space: pre "> </span> return weight; <span style =" white-space: pre "></ span >}< span style =" white-space: pre "> </span> public void setWeight (Double weight) {<span style =" white-space: pre "> </span> this. weight = weight; <span style = "white-space: pre"> </span >}< span style = "white-space: pre "> </span> public String getName () {<span style =" white-space: pre "> </span> return name; <span style = "white-space: pre"> </span >}< span style = "white-space: pre "> </span> <span style =" white-space: pre "> </span> public Sex getSex () {<span style =" white-space: pre "> </span> return sex; <span style =" white-space: pre "> </span >}< span style =" white-space: pre "> </span> <span style =" white-space: pre "> </span>/** <span style =" white-space: pre "> </span> * @ param name the name to set <span style =" white-space: pre "> </span> */<span style =" white-space: pre "> </span> public void setName (String name) {<span style = "white-space: pre"> </span> this. name = name; <span style = "white-space: pre"> </span >}< span style = "white-space: pre "> </span>/** <span style =" white-space: pre "> </span> * @ param sex the sex to set <span style =" white-space: pre "> </span> */<span style =" white-space: pre "> </span> public void setSex (Sex) {<span style = "white-space: pre"> </span> this. sex = sex; <span style = "white-space: pre"> </span >}< span style = "white-space: pre "> </span>} dynamically creates an interception class: [java] import org. interceptor. lazyInitializer; import com. entity. user; public class Test {public static void main (String [] args) throws Exception {User user = (User) LazyInitializer. createBean (User. class); user. setName ("James ");}}

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.