AOP为Aspect Oriented Programming 的缩写,意为:面向切面编程,他是一种可以在不修改原来的核心代码的情况下给程序动态统一进行增强的一种技术。
SpringAOP:批量对Spring容器中的bean的方法做增强,并且这种增强不会与原来方法中的代码耦合。
1.添加依赖
<!--SpringIOC相关依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--AOP相关依赖-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
2.开启包扫描和开启AOP注解支持 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: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 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.sangeng"></context:component-scan>
<!--开启aop注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
3.创建切面类 MyAspect.java
package com.sangeng.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component
@Aspect
public class MyAspect {
@Pointcut("execution(* com.sangeng.service..*.*(..))")
public void pt(){
}
// @Before("pt()")
// public void methodbefore(){
// System.out.println("方法被调用了");
// }
// @Before("pt()")
public void before(JoinPoint joinPoint){
System.out.println("before");
}
// @AfterReturning(value = "pt()",returning = "ret")
public void afterReturning(JoinPoint joinPoint,Object ret){
System.out.println("afterReturning");
}
// @After("pt()")
public void after(JoinPoint joinPoint){
System.out.println("after");
}
// @AfterThrowing(value = "pt()",throwing = "e")
public void afterThrowing(JoinPoint joinPoint,Throwable e){
String message = e.getMessage();
System.out.println("afterThrowing");
}
@Around("pt()")
public Object around(ProceedingJoinPoint pjp){
//获取参数
Object[] args = pjp.getArgs();
MethodSignature signature = (MethodSignature) pjp.getSignature();
Object target = pjp.getTarget();
Object ret = null;
try {
ret = pjp.proceed();//目标方法的执行
//ret就是被增强方法的返回值
System.out.println(ret);
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println(throwable.getMessage());
}
// System.out.println(pjp);
return ret;
}
}
4.创建dao UserDao.java
package com.sangeng.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void test(){
}
}
5.创建service UserService.java
package com.sangeng.service;
import com.sangeng.aspect.InvokeLog;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void deleteAll(){
System.out.println("UserService中deleteAll的核心代码");
}
// @InvokeLog
public void updateById(Integer id){
System.out.println("UserService中updateById的核心代码");
}
public int updateById(Integer id,String name,double price){
// System.out.println(1/0);
System.out.println("UserService中updateById的核心代码");
return id;
}
}
6.测试 Demo.java
package com.sangeng;
import com.sangeng.service.PhoneService;
import com.sangeng.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[] args) {
//创建容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取对象
// PhoneService phoneService = applicationContext.getBean(PhoneService.class);
UserService userService = applicationContext.getBean(UserService.class);
//调用方法
// phoneService.deleteAll();
// userService.deleteAll();
// userService.updateById(10);
// userService.deleteAll();
int ret = userService.updateById(2, "三更", 50000000);
System.out.println(ret);
}
}