spring-aop-切点表达式的抽取。

1.

spring-aop-切点表达式的抽取。_第1张图片

---jar包文件



    
        Spring
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    spring03-aop
    

        
            junit
            junit
            4.12
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
        
            org.aspectj
            aspectjweaver
            1.8.4
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        


    


com.liuboss.aop.TargetInterface  接口

package com.liuboss.aop;

public interface TargetInterface {
    public void save();
}

com.liuboss.aop.Target  实现接口方法

package com.liuboss.aop;

public class Target implements TargetInterface {

    public void save() {
        System.out.println("TargetInterface...save running");
    }
}

com.liuboss.aop.MyAspect  实现切面的方法

package com.liuboss.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAspect {
    public void before(){
        System.out.println("前值增强");
    }
    public void after(){
        System.out.println("后置增强");
    }
    //ProceedingJoinPoint 正在执行的连接点-切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前-增强");
        //添加切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后-增强");
        return proceed;
    }
    public void agterThrowing(){
        System.out.println("抛出异常增强。。。。。。");
    }

}

---spring配置文件进行配置





    
    
    
    
    
    
    
        
        
            
            
            
            
            
            

            
            



        
    


---测试文件进行测试

package com.liuboss.demo;

import com.liuboss.aop.TargetInterface;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
    @Autowired
    private TargetInterface target;

    @org.junit.Test
    public void test1() {
        target.save();
    }


}

 

spring-aop-切点表达式的抽取。_第2张图片

spring-aop-切点表达式的抽取。_第3张图片

spring-aop-切点表达式的抽取。_第4张图片

-------注解配置切点表达式。

pom文件



    
        Spring
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    spring03-aop
    

        
            junit
            junit
            4.12
        
        
            org.springframework
            spring-context
            5.0.5.RELEASE
        
        
        
            org.aspectj
            aspectjweaver
            1.8.4
        
        
            org.springframework
            spring-test
            5.0.5.RELEASE
        


    


 ---com.liuboss.anno.TargetInterface   创建接口

package com.liuboss.anno;

public interface TargetInterface {
    public void save();
}

com.liuboss.anno.Target  实现接口的方法

package com.liuboss.anno;

import org.springframework.stereotype.Component;

@Component("target")
public class Target implements TargetInterface {

    public void save() {
        System.out.println("TargetInterface...save running");
    }
}

com.liuboss.anno.MyAspect  实现其中的方法

package com.liuboss.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component("MyAspect")
//声明为切面 标注当前MyAspect是一个切面类
@Aspect
public class MyAspect {
    @Before("execution(* com.liuboss.anno.*.*(..))")
    public void before(){

        System.out.println("前值增强");
    }
    @After("execution(* com.liuboss.anno.*.*(..))")
    public void after(){
        System.out.println("后置增强");
    }
    //ProceedingJoinPoint 正在执行的连接点-切点
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前-增强");
        //添加切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后-增强");
        return proceed;
    }
    public void agterThrowing(){
        System.out.println("抛出异常增强。。。。。。");
    }

}

spring文件进行配置   applicationContext-anno.xml


    
    
    

spring-aop-切点表达式的抽取。_第5张图片

spring-aop-切点表达式的抽取。_第6张图片

切点表达式的抽取

com.liuboss.anno.MyAspect

package com.liuboss.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("MyAspect")
//声明为切面 标注当前MyAspect是一个切面类
@Aspect
public class MyAspect {
    //@Before("execution(* com.liuboss.anno.*.*(..))")
    @Around("MyAspect.pointcut()")
    public void before(){

        System.out.println("前值增强");
    }
    //@After("execution(* com.liuboss.anno.*.*(..))")
    @After("MyAspect.pointcut()")
    public void after(){
        System.out.println("后置增强");
    }
    //ProceedingJoinPoint 正在执行的连接点-切点
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前-增强");
        //添加切点方法
        Object proceed = pjp.proceed();
        System.out.println("环绕后-增强");
        return proceed;
    }
    public void agterThrowing(){
        System.out.println("抛出异常增强。。。。。。");
    }
    @Pointcut("execution(* com.liuboss.anno.*.*(..))")
    public void pointcut(){

    }
}

spring-aop-切点表达式的抽取。_第7张图片

spring-aop-切点表达式的抽取。_第8张图片

重点是xml方式

spring-aop-切点表达式的抽取。_第9张图片

spring-aop-切点表达式的抽取。_第10张图片

 

 

 

 

你可能感兴趣的:(spring)