目录
图解:
applicationCotext.xml:
- 这里三种方法:原生spring API接口实现 自定义类 注解
<?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: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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.gao.service.UserServiceImpl"/>
<bean id="log" class="com.gao.log.Log"/>
<bean id="afterLog" class="com.gao.log.AfterLog"/>
<!--方式一:使用原生Spring API接口(主要是SpringAPI接口实现)-->
<!--配置aop :需要导入aop的约束-->
<aop:config>
<!--切入点: expression:表达式(要执行的位置! * * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.gao.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加 执行日志-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
<!-- 方式二:自定义类(主要是切面定义)-->
<bean id="diy" class="com.gao.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref要引入的类-->
<aop:aspect ref="diy">
<!--切入点 execution表达式 每个*有自己的意思-->
<aop:pointcut id="point" expression="execution(* com.gao.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
<!--方式三-->
<bean id="annotationPointCut" class="com.gao.diy.AnnotationPointCut"/>
<!--开启注解支持! JDK(默认 proxy-target-class="false") cglib (proxy-target-class="true)-->
<!--<aop:aspectj-autoproxy proxy-target-class="true"/>-->
<aop:aspectj-autoproxy/>
</beans>
service被插入切面的类:
package com.gao.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
package com.gao.service;
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("更新了一个用户");
}
@Override
public void select() {
System.out.println("查询了一个用户");
}
}
main:
import com.gao.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理的是接口 注意点
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
原生方式:
log包下的Log和AfterLog
package com.gao.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
//执行被代理类前日志
public class Log implements MethodBeforeAdvice {
//method:要执行的目标方法
//args:参数
//target:
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
package com.gao.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
//执行被代理类后日志
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为"+returnValue);
}
}
自定义类方式:
diy包下的DiyPointCut
package com.gao.diy;
public class DiyPointCut {
public void before(){
System.out.println("=====方法执行前======");
}
public void after(){
System.out.println("======方法执行后=====");
}
}
注解:
diy包下的AnnotationpointCut
package com.gao.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//方式三:使用注解方式实现aop
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.gao.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("====方法执行前(调用before)=====");
}
@After("execution(* com.gao.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行后(调用after)=====");
}
//在环绕增强中,可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.gao.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) {
System.out.println("~~~~~~~环绕前~~~~~~~");
Signature signature = jp.getSignature();//获得签名
System.out.println("signature:"+signature);
//执行方法
Object proceed=null;
try {
proceed = jp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("~~~~~~~~~环绕后~~~~~~~~");
System.out.println("proceed = "+proceed);
}
}