码迷,mamicode.com
首页 > 其他好文 > 详细

四种常用的通知类型和环绕通知(注解)

时间:2020-02-22 15:58:48      阅读:555      评论:0      收藏:0      [点我收藏+]

标签:实体   return   runtime   版本   ann   proc   ssr   sch   execution   

1、maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ly.spring</groupId>
    <artifactId>spring05</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--用于解析切入点表达式-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.6</version>
        </dependency>
        <!--用于整合junit-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>13</source>
                    <target>13</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

 

2、实体类

package com.ly.spring.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private String accountName;

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    @Override
    public String toString() {
        return "Account{" +
                "accountName=‘" + accountName + ‘\‘‘ +
                ‘}‘;
    }
}

3、service接口

package com.ly.spring.service;

import com.ly.spring.domain.Account;

import java.util.List;

public interface IAccountService {
    public List<Account> findAll();
}

 

4、service实现类

package com.ly.spring.service.impl;

import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Override
    public List<Account> findAll() {
        System.out.println("AccountServiceImpl---findAll");
        List<Account> list = new ArrayList<>();
        Account account = new Account();
        account.setAccountName("hehe");
        list.add(account);
        return list;
    }
}

5、通知类

package com.ly.spring.utils;

import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("logUtil")
//@Aspect:指定此类为切面通知类
@Aspect
public class LogUtil {
    //@Pointcut:配置切入点
    @Pointcut("execution(* com.ly.spring.service.impl.*.*(..))")
    private void pointCut1() {}

    //@Before:配置前置通知方法并指定切入点
    @Before("pointCut1()")
    public void beforeFunc() {
        System.out.println("---前置通知---");
    }

    //@AfterReturning:配置后置通知方法并指定切入点
    @AfterReturning("pointCut1()")
    public void afterReturnFunc() {
        System.out.println("---后置通知---");
    }

    //@AfterThrowing:配置异常通知方法并指定切入点
    @AfterThrowing("pointCut1()")
    public void afterThrowFunc() {
        System.out.println("---异常通知---");
    }

    //@After:配置最终通知方法并指定切入点
    @After("pointCut1()")
    public void afterFunc() {
        System.out.println("--最终通知--");
    }
}

6、spring配置文件

<?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"
       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">
    <!--配置注解扫描的包-->
    <context:component-scan base-package="com.ly.spring"></context:component-scan>
    <!--开启AOP注解配置-->
    <!--
    AOP注解配置说明:
    1、在用注解配置使用4个常用通知时,最终通知会在后置通知前执行
    2、若是要用纯注解方式可以在配置类上加@EnableAspectJAutoProxy注解
    -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

 

7、测试类

package com.ly.spring.test;

import com.ly.spring.domain.Account;
import com.ly.spring.service.IAccountService;
import org.junit.Test;
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;

import java.util.List;

//替换junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置文件的位置
@ContextConfiguration(locations = "classpath:bean.xml")
public class MainTest {
    @Autowired
    private IAccountService accountService;
    @Test
    public void test1() {
        List<Account> result = accountService.findAll();
        System.out.println(result);
    }
}

 

8、基于注解的环绕通知,只需在上面的基础上修改通知类

package com.ly.spring.utils;

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

@Component("logUtil")
//@Aspect:指定此类为切面通知类
@Aspect
public class LogUtil {
    //@Pointcut:配置切入点
    @Pointcut("execution(* com.ly.spring.service.impl.*.*(..))")
    private void pointCut1() {}
    public void beforeFunc() {
        System.out.println("---前置通知---");
    }
    public void afterReturnFunc() {
        System.out.println("---后置通知---");
    }
    public void afterThrowFunc() {
        System.out.println("---异常通知---");
    }
    public void afterFunc() {
        System.out.println("--最终通知--");
    }
    //@Around:配置环绕通知方法并指定切入点
    @Around("pointCut1()")
    public Object arroundFunc(ProceedingJoinPoint pjp) {
        try {
            Object result = null;
            Object[] args = pjp.getArgs();
            beforeFunc();//前置通知
            result = pjp.proceed(args);
            afterReturnFunc();//后置通知
            return result;
        }catch (Throwable t) {
            afterThrowFunc();//异常通知
            throw new RuntimeException(t);
        }finally {
            afterFunc();//最终通知
        }
    }
}

 

四种常用的通知类型和环绕通知(注解)

标签:实体   return   runtime   版本   ann   proc   ssr   sch   execution   

原文地址:https://www.cnblogs.com/liuyang-520/p/12345468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!