How do I extend the classes in a third-party jar package In a Java project?

Source: Internet
Author: User
Tags aop

There are times when you are not very satisfied with third parties getting the classes in the jar package, and want to do some expansion based on the actual situation. If a third-party jar package already provides some extensible classes, such as Interceptor,filter or other classes, then use native comparison insurance. But if it is not provided, it will take some unusual road. Here I have summed up some feasible methods, some elegant, some are the last resort.

First, inherit the class you want to extend, overriding the method you want to extend

This approach is better and conforms to the idea of object-oriented. The other action is smaller, the risk is not very big. However, this method has the premise that the extended class can be replaced with this subclass in the extension point. For example, in the process of bean configuration, it can be injected with a subclass where the class is needed, rather than injecting the extended class. This means that the classes that originally wanted to extend are all directly new, and our newly written subclasses simply cannot be used where they need to be expanded.

Duplicate a class that you want to extend, create a class with the same name as the package in your project

This approach is what I see on the web, taking advantage of the class loading priority, which is that the class loading priority in the Web-inf directory is higher than the priority of the Jar package class in lib.

Third, using ASM and other bytecode modification tool, directly modify the byte code of this class

This is the ultimate approach, just modify the bytecode of this class to look like you want to extend it. The idea is quite straightforward, and the downside is that you are familiar with bytecode and are familiar with ASM tools. Second, the risk of this approach is relatively large, if you put forward this idea, it is likely to be pass off.

In fact, these are the AOP to do, AOP I touch less, configuration up a little trouble, and sometimes not necessarily give you configuration.

Summarize:

How do I extend the classes in a third-party jar package In a Java project?

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.