Spring framework Article 5: Spring and AOP; spring Article 5: aop

Source: Internet
Author: User

Spring framework Article 5: Spring and AOP; spring Article 5: aop
I. AOP Overview

AOP (Aspect Orient Programming) is a supplement to Object-Oriented Programming (OOP. Object-Oriented Programming considers the program structure from the static point of view, while Aspect-Oriented Programming considers the program running process from the dynamic point of view.

The bottom layer of AOP is implemented in the dynamic proxy mode. Two proxies are used: JDK dynamic proxy and CGLIB dynamic proxy.

For Aspect-Oriented Programming, cross-business logic is encapsulated into a aspect, and the aspect is woven into the main business logic using the function of the AOP container. Cross-business logic refers to common code that is irrelevant to the main business logic. Such as security check, transactions, logs, etc.

If AOP is not used, code tangle occurs, that is, the cross-business logic is mixed with the main business logic, which leads to confusion between the main business logic.

Ii. Notice Advice1, notice details (1) pre-notification MethodBeforeAdvice

Define the pre-notification and implement the MethodBeforeAdvice interface. This interface has a before () method that will be executed before the target method is executed.

Features of pre-notification:

1. Execute the command before executing the target method.

2. The pre-notification Code cannot prevent the execution of the target method without changing the execution process of the target method.

3. Do not change the execution result of the target method.

Example:

Create IService interface:

package com.ietree.spring.basic.aop.beforeadvice;public interface IService {    void doFirst();    void doSecond();}

Interface creation implementation class:

Package com. ietree. spring. basic. aop. beforeadvice; public class SomeServiceImpl implements IService {@ Override public void doFirst () {System. out. println ("executing the doFirst () method") ;}@ Override public void doSecond () {System. out. println ("executing the doFirst () method ");}}

Create a pre-notification class MyMethodBeforeAdvice, which must implement the MethodBeforeAdvice interface:

Package com. ietree. spring. basic. aop. beforeadvice; import java. lang. reflect. method; import org. springframework. aop. methodBeforeAdvice;/*** pre-notification ** @ author Root */public class MyMethodBeforeAdvice implements MethodBeforeAdvice {// The current method is executed before the target method is executed // method: target Method // args: target method parameter list // target: target Object @ Override public void before (Method method, Object [] args, Object target) throws Throwable {// The enhancement code for the target method is written here in System. out. println ("execution pre-notification... ");}}

Configure the XML 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" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! -- 1. register the target object --> <bean id = "someService" class = "com. ietree. spring. basic. aop. beforeadvice. SomeServiceImpl"/> <! -- 2. Registration aspect: notification --> <bean id = "myAdvice" class = "com. ietree. spring. basic. aop. beforeadvice. MyMethodBeforeAdvice"/> <! -- 3. Generate proxy object --> <bean id = "serviceProxy" class = "org. springframework. aop. framework. ProxyFactoryBean"> <! -- Specify the target object --> <! -- <Property name = "targetName" value = "someService"/> --> <property name = "target" ref = "someService"/> <! -- Specify a plane --> <property name = "interceptorNames" value = "myAdvice"/> </bean> </beans>

Test:

package com.ietree.spring.basic.aop.beforeadvice;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {    @Test    public void test() {                String resource = "com/ietree/spring/basic/aop/beforeadvice/applicationContext.xml";                ApplicationContext ac = new ClassPathXmlApplicationContext(resource);            IService service = (IService)ac.getBean("serviceProxy");                service.doFirst();        System.out.println("==============");        service.doSecond();    }}

Output:

Execute the pre-notification... execute the doFirst () method ============== execute the pre-notification... execute the doFirst () method

Note: import the spring-aop-4.3.9.RELEASE.jar package before execution

(2) Post-Notification AfterReturningAdvice

 

(3) MethodInterceptor

 

(4) ThrowsAdvice

 

Iii. Advisor

 

Iv. Automatic proxy Generator

 

V. Implementation of AspectJ on AOP

 

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.