AOP配置(注解方式)入门案例演示

1、AOP配置

 

2、注解开发AOP制作步骤

在XML格式基础上

  • 导入坐标(伴随spring-context坐标导入已经依赖导入完成)

  • 开启AOP注解支持

  • 配置切面@Aspect

  • 定义专用的切入点方法,并配置切入点@Pointcut

  • 为通知方法配置通知类型及对应切入点@Before

 

3、注解开发AOP注意事项

1.切入点最终体现为一个方法,无参无返回值,无实际方法体内容,但不能是抽象方法

2.引用切入点时必须使用方法调用名称,方法后面的()不能省略

3.切面类中定义的切入点只能在当前类中使用,如果想引用其他类中定义的切入点使用“类名.方法名()”引用

4.可以在通知类型注解后添加参数,实现XML配置中的属性,例如after-returning后的returning属性

 

 4、入门案例

4.1、项目结构

 4.2、pom.xml

<?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.itheima</groupId>
    <artifactId>Spring_day03_02_AOP注解</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
    </dependencies>

</project>

4.3、applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">
    <context:component-scan base-package="com.itheima"/>
    <aop:aspectj-autoproxy/>
    <!--<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>-->

</beans>

4.4UserService接口代码

package com.itheima.service;

public interface UserService {
    int save(int i, int m);
}

4.4UserServiceImpl实现类

package com.itheima.service.impl;

import com.itheima.service.UserService;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
    public int save(int i, int m) {
        System.out.println("user service running..." + i + "," + m);
        return 100;
    }
}

4.5、AOPAdvice

package com.itheima.aop;

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

@Component
@Aspect
public class AOPAdvice {
    @Pointcut("execution(* *..*(..))")
    public void pt() {
    }

    @Before("pt()")
    public void before() {
        System.out.println("前置before...");
    }

    @After("pt()")
    public void after() {
        System.out.println("后置after...");
    }

    @AfterReturning("pt()")
    public void afterReturning() {
        System.out.println("返回后afterReturning...");
    }

    @AfterThrowing("pt()")
    public void afterThrowing() {
        System.out.println("抛出异常后afterThrowing...");
    }

    @Around("pt()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("环绕前around before...");
        Object ret = pjp.proceed();
        System.out.println("环绕后around after...");
        return ret;
    }
}

4.6、App代码

import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) ctx.getBean("userService");
        int ret = userService.save(666, 888);
        System.out.println("应用程序调用执行结果...." + ret);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠然予夏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值