Spring AOP and Spring AOP

Source: Internet
Author: User

Spring AOP and Spring AOP

What is AOP (Aspect Oriented Programming ):

We have always called Object Oriented Programming object-oriented Programming as a vertical Programming method. It extends to the end in a line, and can be imagined as an endless path. There is often a situation where many different paths are intertwined, which means that the same processing logic exists between different objects. In order to make this part of logic reusable, better understanding, and more convenient maintenance, the AOP (Aspect Oriented Programming) Oriented Programming idea emerged, which is called the horizontal Programming method.

How to Implement AOP:

The idea of AOP programming is implemented through the agent technology. For details, refer to the design pattern and study the java Dynamic proxy technology.

There are two methods to implement proxy in Spring:

1. JDK dynamic Proxy: the proxy object must be the implementation of an interface. It creates an interface implementation class during runtime to proxy the target object.

2. CHLIB Proxy: The implementation principle is similar to JDK dynamic proxy, but the proxy object generated during runtime is a subclass of the target class extension. DGLIB is an efficient code generation package, the underlying layer relies on the ASM operation bytecode, and the performance is better than that of JDK. However, the final methods and classes in the class cannot be used as proxies. (Put the CGLIB binary distribution package under classpath ).

The following is a simple example:

// Java code @ Aspectpublic class AspectJTest {@ Pointcut ("execution (**. test (..)) ") public void test () {System. out. println ("test") ;}@ Before ("test ()") public void beforeTest () {System. out. println ("before") ;}@ After ("test ()") public void afterTest () {System. out. println ("111"); object = null; try {o = p. proceed ();} catch (Exception e) {e. printStackTrace ();} System. out. println ("2222") ;}@ Around ("test ()") public void aroundTest (ProceedingJoinPoint p) {System. out. println ("");}}

// Configuration file

<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/beans/spring-aop-3.0.xsd>
<! -- Start support for @ AspectJ annotation -->
<Aop: aspectj-autoproxy/>
</Beans>

Proxy principles for Implementing AOP:

If the target object implements the interface, the dynamic proxy of JDK is used by default to implement AOP.

If the target object implements the interface, you can force it to implement AOP Using CGLIB, but add <aop: aspectj-autoproxy proxy-target-class = "true"> to the configuration file.

If the object does not implement an interface, the CGLIB library must be used. Spring will automatically convert between the JDK proxy and CGLIB.

 

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.