Spring使用注解的方式使用aop

Spring-AOP

使用注解的方式使用aop

一、在配置文件中开启注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.wfb"/>
    <aop:aspectj-autoproxy/>

</beans>

二、创建项目结构

在这里插入图片描述

(一)创建dao

PersonDaoImpl.java

package com.wfb.dao.impl;

import com.wfb.dao.PersonDao;
import org.springframework.stereotype.Repository;

/**
 * @Author WFB
 * @Time 2021/4/1 8:30
 */
@Repository
public class PersonDaoImpl implements PersonDao {

    @Override
    public void say() throws Exception {
        System.out.println("你好呀!");
    }
    @Override
    public void see(){
        System.out.println("看到你了!");
    }
}

PersonDao.java

package com.wfb.dao;

/**
 * @Author WFB
 * @Time 2021/4/1 8:29
 */
public interface PersonDao {
    public void say() throws Exception;
    public void see();
}

(一)创建aspect

在其中加入通知的方法,

@Aspect注解:

​ 这是一个通知类

@Pointcut注解:

​ 切入点execution(* com.wfb.dao.impl.PersonDaoImpl.*())第一个*代表访问修饰符(可以省略)第二个*代表返回值,后边加类的全路径,可以使用*代替(代表所有类),括号前面的*代表需要增强的方法名称括号内..代表有多个参数。

@Before()注解

​ 需要提供参数”切入点“,前置通知

@Around( )注解:

​ 需要提供参数”切入点“,环绕通知

@AfterReturning()注解

​ 需要提供参数”切入点“,返回通知

@AfterThrowing()注解

​ 需要提供参数”切入点“,异常通知

@After()注解

需要提供参数”切入点“,后置通知,无论是否发生异常都会执行。

PersonDaoAspect.java

package com.wfb.Aspect;

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

/**
 * @Author WFB
 * @Time 2021/4/1 8:33
 */
@Aspect
@Component
public class PersonDaoAspect {
    @Pointcut("execution(* com.wfb.dao.impl.PersonDaoImpl.*())")
    private void point(){

    }
    @Before("point()")
    public void before(){
        System.out.println("before前置通知");
    }
    @Around("point()" )
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("around环绕通知前");
        Object o = point.proceed();
        System.out.println("around环绕通知后");
        return o;
    }
    @AfterReturning("point()")
    public void afterReturning(){
        System.out.println("AfterReturning返回通知");
    }
    @AfterThrowing("point()")
    public void afterThrowing(){
        System.out.println("AfterThrowing异常通知");
    }
    @After("point()")
    public void after(){
        System.out.println("After后置通知");
    }
}

(三)测试
package com.wfb.test;

import com.wfb.dao.PersonDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author WFB
 * @Time 2021/4/1 8:43
 */
public class Test {
    public static void main(String[] args) throws Exception {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonDao personDao= (PersonDao) context.getBean("personDaoImpl");
        personDao.say();
        System.out.println("------------------------");
        personDao.see();
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值