Spring-Aop

1.什么是AOP

AOP(Aspect Oriented Programming,面向切面编程),通过提供另一种思考程序的方式来补充OOP(Object Oriented Programming,面向对象编程)。AOP是横向抽取,OOP是纵向抽象。
切面可以用于事务管理、日志等方面的模块化

2.AOP核心概念

Aspect(切面)
Join Point(连接点)
Advice(通知/增强)
Pointcut(切点)
Introduction(引入)
Target Object(目标对象)
AOP Proxy(AOP代理)
Weaving(织入)

其中,Advice的主要类型有:

Before Advice(前置通知)
After Returning Advice(返回后通知)
After Throwing Advice(抛出异常后通知)
After (finally)Advice(最后通知)
Around Advice(环绕通知)

  • 注 :切入点和连接点的匹配,是AOP的关键

Hello的前置增强练习

  • pom.xml中添加AOP相关依赖



    4.0.0

    com.spring
    aop
    1.0-SNAPSHOT

    aop
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
        5.1.5.RELEASE
        1.9.2
        4.12
        1.2.17
        1.7.12
    

    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
        
            org.aspectj
            aspectjrt
            ${aspectj.version}
        
        
            org.aspectj
            aspectjweaver
            ${aspectj.version}
        
        
        
            org.springframework
            spring-test
            ${spring.version}
        
        
        
            junit
            junit
            ${junit.version}
            test
        
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
    

    
        
            
                
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-jar-plugin
                    3.0.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
                
                
                    maven-site-plugin
                    3.7.1
                
                
                    maven-project-info-reports-plugin
                    3.0.0
                
            
        
    


  • Hello接口和实现类
package com.spring.aop;

public interface Hello {
    String sayHello();
}

package com.spring.aop;

public class HelloImpl implements Hello {
    @Override
    public String sayHello() {
        return "Spring AOP";
    }
}

  • MyBefore类
package com.spring.aop;

/**
 * 用戶自定义前置增强类
 */
public class MyBefore {
    public void beforeMethod() {
        System.out.println("This is a before method...");
    }
 }

  • 配置文件
    
    
    
        
            
            
        
    

  • 应用主类
package com.spring.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloApp {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello hello = (Hello) ac.getBean("hello");
        System.out.println(hello.sayHello());
    }
}

  • 运行结果

    image

你可能感兴趣的:(Spring-Aop)