JDK dynamic proxy and cglib agent in Spring AOP

Source: Internet
Author: User
Tags aop
1. Application in spring

The joinpoint that spring weaves advice into the target object is dynamic at run time. There may be two ways in which the JDK dynamic agent and the Cglib agent can be used. Spring will switch between the two depending on the situation.

The actual situation is as follows:

Ø if the target object implements the interface, then spring defaults to implement AOP with the JDK dynamic proxy

Ø if the target object implements an interface, you can also force the use of the Cglib build agent to implement AOP

Ø if the target object does not implement an interface, then you must use the Cglib build agent to implement AOP instance 1:

The target object implements the interface:

Package com.bjpowernode.spring;

public class Usermanagerimpl//{
		implements Usermanager {public

	void AddUser (string username, string password) {
		//CheckSecurity ();
		System.out.println ("---------usermanagerimpl.add ()--------");
	}

You can see that the JDK dynamic agent is used:



Example 2:

The target object does not implement an interface:

Package com.bjpowernode.spring;

public class Usermanagerimpl {
	//implements Usermanager {public

	void AddUser (string username, string password) {
		//CheckSecurity ();
		System.out.println ("---------usermanagerimpl.add ()--------");
	}

You can see that the Cglib agent is used:



2, JDK dynamic agent and cglib agent difference

*JDK Dynamic Proxy proxy for the class that implements the interface

The *cglib proxy can generate a subclass of the generic class proxy, mainly for the specified class, because it is inherited, so it is best not to use the final declaration for our target class. The function of providing a proxy class is to allow classes that have not implemented interfaces from previous or third parties to be notified, and methods that are marked final cannot be notified.


3. How to force the use of cglib generation agent

* Join Cglib Library: Spring_home/lib/cglib/*.jar

* Add the following configuration to the spring configuration file, forcing the use of the Cglib proxy

<aop:aspectj-autoproxyproxy-target-class= "true"/>


4, Summary

The performance of the dynamic proxy object created by Cglib is much higher than that of the proxy object created by JDK, but Cglib spends more time creating proxy objects than JDK dynamic proxies, so for singleton proxy objects or proxies with instance pools, Because it is not necessary to create new instances frequently, it is more suitable for cglib dynamic agent technology, whereas it is suitable for JDK dynamic proxy technology. In addition, because Cglib uses dynamically creating subclasses to generate proxy objects, methods such as Final,private in the target class cannot be processed. Therefore, we need to choose what kind of agent to use according to the actual situation.

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.