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