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