Java framework programming (5)-Application of cglib

Source: Internet
Author: User
Reflection, proxy, and metadata are the three most powerful features of Java. Together with cglib (code generation Library) and ASM, Java is not Ruby, and python is awesome, can make a powerful framework.
Proxy can be seen as micro-AOP. It understands that it provides a third code encapsulation path out of inheritance and delegation. As long as you have enough imagination, it can be very fun, in spring source code, proxy can be used very casually. I am very jealous. Unfortunately, the proxy must be based on the interface. Therefore, spring uses proxy Based on interfaces, otherwise cglib is used. AOP? Generally, aspectj won't be involved if it's not the level of compoent.

The enhancer of cglib is amazing, and it will be finished when one page is used up.
The principle is to use enhancer to generate a subclass of the original class and set callback to proxy. Then, every method call of the original class will be converted into calling intercept () of proxy that implements the methodinterceptor interface () function: Public object intercept (Object o, method, object [] ARGs, methodproxy proxy)

In the intercept () function, you can execute object result = proxy. invokesuper (O, argS); to execute the original function, add your own stuff before and after the execution, change its parameter value, you can also go across the sea, completely dry. To put it bluntly, it is the und advice in AOP.

Before the emergence of AOP, the classic design mode in this field was decorator, as was the design of Java Io stream. however, for each DAO, writing the decorator function for each method will be dead, so the advantage of using cglib is to intercept all methods at a time.

In addition to enhancer, cglib also has bulkbean and transform, which are the basis of Hibernate Persistence. However, the document is poor and I haven't checked how to use it yet.

1. In AOP, one hundred times a one hundred times log aspect is implemented in cglib as follows:


Public class logdaoproxy implements methodinterceptor
{
Private logger log = logger. getlogger (logdaoproxy. Class );
Private enhancer = new enhancer ();
// Return the subclass of Dao
Public object getdao (class clz)
{
Enhancer. setsuperclass (clz );
Enhancer. setcallback (this );
Return enhancer. Create ();
}
// Default Interception Method
Public object intercept (Object o, method, object [] ARGs, methodproxy proxy) throws throwable
{
Log.info ("call log method" + method. getname ());
Object result = proxy. invokesuper (O, argS );
Return result;
}
}

Application code: logdaoproxy proxy = new logdaoproxy ();
Goodsdao Dao = (goodsdao) proxy. getdao (goodsdao. Class );
Dao. insert (goods );

2. The modified advanced decorator should be added under spring management.
In the preceding example, return enhancer. Create (); is used to create a subclass instance. However, in spring management, some bean instances must be created and managed by spring instead of enhancer. Therefore, I have slightly modified the above usage so that it is truly a proxy role. Please compare it with the section in the black text

Public class logdaoproxy implements methodinterceptor
{
Private logger log = logger. getlogger (logdaoproxy. Class );
Private object Dao = NULL ;
Private enhancer = new enhancer ();
// Return the subclass of Dao
Public object getdao (class clz, object Dao)
{
This. Dao = Dao;
Enhancer. setsuperclass (clz );
Enhancer. setcallback (this );
Return enhancer. Create ();
}
// Default Interception Method
Public object intercept (Object o, method, object [] ARGs, methodproxy proxy) throws throwable
{
Log.info ("call log method" + method. getname ());
Object result = Proxy. Invoke (DAO, argS );
Return result;
}
}

It can be seen that in the original mode, the DAO is created by enhancer during getdao (), and the DaO created by enhancer is returned by the object o parameter when intercept is called.
In the new mode, Dao is passed in from the outside when getdao (), enhancer. create () returns a proxy. when intercept is called, the previously passed-in Dao is used for operations, while the proxy passed in by the object o parameter is ignored.

Sorry, the signature of methodproxy In the Intercept function is fixed, that is, if the customer calls Foo (string), you cannot use proxy. invoke to steal it into Foo (string, string );

Series of articles:
Java framework writing (1) -- Preface
Java framework programming (2)-boring usage of generics and platforms for wiping Methods
Java framework programming (3) -- fragment about reflection
Java framework programming (4) -- Annotation vs XML vs interface the latest round
Java framework programming (5) -- cglib Application
Framework programming in Java (6) -- ASM (to be written)

Related Article

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.