AOP注解开发

切入点配置

package com.heima.spring.Aop_注解入门;

public class user {
    public void show() {
        System.out.println("显示用户");
    }
    public void insert() {
        System.out.println("插入用户"); 
        //System.out.println(1/0);
    }
    public String delete() {
        System.out.println("删除用户"); 
        return "小明";
    }
    public void update() {
        System.out.println("修改用户");     
    }
}


切面配置

package com.heima.spring.Aop_注解入门;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyaspectXml {
    
    @Before(value="execution(* com.heima.spring.Aop_注解入门.user.show(..))")
    public void checkPri(JoinPoint joinPoint) {
        System.out.println("权限校验"+"增强方法为"+joinPoint);
    }
    
    @AfterReturning(value="execution(* com.heima.spring.Aop_注解入门.user.delete(..))",returning="res")
    public void log(Object res) {
        System.out.println("日志记录"+"方法返回值"+res);
    }
    
    
    @Around(value="execution(* com.heima.spring.Aop_注解入门.user.delete(..))")
    public Object around(ProceedingJoinPoint pjJoinPoint) throws Throwable {
        System.out.println("环绕通知前");
        Object obj = pjJoinPoint.proceed();
        System.out.println("环绕通知后");
        return obj;
    }
    @AfterThrowing(value="execution(* com.heima.spring.Aop_注解入门.user.update(..))",throwing="ex")
    public void insertEXC(Throwable ex) {
        System.out.println("出现异常"+ex.getMessage());
    }
    
    @After(value="MyaspectXml.point1()")
    public void after() {
        System.out.println("插入之后执行的方法,无论如何都进行");
    }
    
    //切入点注解  (用来代替通知注解中的value="execution(* com.heima.spring.Aop_注解入门.user.update(..))")
    @Pointcut(value="execution(* com.heima.spring.Aop_注解入门.user.update(..))")
    private void point1(){
        
    }
}

测试类配置

package com.heima.spring.Aop_注解入门;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
    @Resource(name="userimp")
    user user;
    @Test
    public void demo(){
        user.insert();
        user.show();
        user.delete();
        user.update();
    }
}

配置文件配置



        
     
    
    
    
    
    


你可能感兴趣的:(AOP注解开发)