欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AOP注解开发

程序员文章站 2022-03-06 21:00:34
...

切入点配置

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();
    }
}

配置文件配置

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <aop:aspectj-autoproxy/>    <!-- 注解型扫描 -->
     <!-- 配置目标对象 -->
    <bean id="userimp" class="com.heima.spring.Aop_注解入门.user"></bean>
    <!-- 将切面交给spring管理 -->
    <bean id="mycheck" class="com.heima.spring.Aop_注解入门.MyaspectXml"></bean>
    
    
</beans>

转载于:https://www.jianshu.com/p/29b3e514b562