0
点赞
收藏
分享

微信扫一扫

SpringAop环绕通知和JoinPoint and ProceedingJoinPoint


Proceedingjoinpoint 和JoinPoint的区别:
Proceedingjoinpoint 继承了JoinPoint,proceed()这个是aop代理链执行的方法。并扩充实现了proceed()方法,用于继续执行连接点。JoinPoint仅能获取相关参数,无法执行连接点。JoinPoint的方法
1.java.lang.Object[] getArgs():获取连接点方法运行时的入参列表;
2.Signature getSignature() :获取连接点的方法签名对象;
3.java.lang.Object getTarget() :获取连接点所在的目标对象;
4.java.lang.Object getThis() :获取代理对象本身;

proceed()有重载,有个带参数的方法,可以修改目标方法的的参数

这几天学习这一块我们应该可以很快写出来下面的代码没有难点

package com.wfg.aop2.dao;

/**
* @author wufagang
* @description
* @date 2021年04月24日 3:03 下午
*/
public interface IndexDao {
public void saveUser(String user);
}

package com.wfg.aop2.dao;


import org.springframework.stereotype.Service;

/**
* @author wufagang
* @description
* @date 2021年04月24日 3:03 下午
*/
@Service
public class IndexDaoImpl implements IndexDao {
@Override
public void saveUser(String user) {
System.out.println("save user" + user);
}
}

package com.wfg.aop2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
* @author wufagang
* @description
* @date 2021年04月24日 3:08 下午
*/
@Configuration
@ComponentScan("com.wfg.aop2")
@EnableAspectJAutoProxy(proxyTargetClass=false)
public class AppConfig {
}

package com.wfg.aop2;

import com.wfg.aop2.dao.IndexDao;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
* @author wufagang
* @description
* @date 2021年04月24日 3:06 下午
*/
@Aspect
@Component
public class IndexAspect {

@Before("this(com.wfg.aop2.dao.IndexDaoImpl)")
public void before(){
System.out.println("before==============");
}

@After("target(com.wfg.aop2.dao.IndexDaoImpl)")
public void after(){
System.out.println("after==============");
}
@Around("target(com.wfg.aop2.dao.IndexDaoImpl)")
public void around(ProceedingJoinPoint joinPoint){
System.out.println("around start");
Object[] args = joinPoint.getArgs();
//修改参数
for (int i = 0; i <args.length ; i++) {
args[i]=args[i]+" update";
}
String thisname = joinPoint.getThis().getClass().getName();
System.out.println(thisname);
String targetName = joinPoint.getTarget().getClass().getName();
System.out.println(targetName);
Signature signature = joinPoint.getSignature();
System.out.println(signature.getName());
try {
joinPoint.proceed(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("around end ");
}
}

package com.wfg.aop2;

import com.wfg.aop2.dao.IndexDao;
import com.wfg.aop2.dao.IndexDaoImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.Proxy;

/**
* @author wufagang
* @description
* @date 2021年04月24日 3:09 下午
*/

public class Test2 {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
IndexDao bean = context.getBean(IndexDao.class);
System.out.println("类名称:"+bean.getClass().getName());
System.out.println("bean instanceof IndexDao :"+(bean instanceof IndexDao));
System.out.println("bean instanceof IndexDaoImpl :"+(bean instanceof IndexDaoImpl));
System.out.println("bean instanceof Proxy :"+(bean instanceof Proxy));
bean.saveUser("zhangsan");
}
}

主要代码:

@Around("target(com.wfg.aop2.dao.IndexDaoImpl)")
public void around(ProceedingJoinPoint joinPoint){
System.out.println("around start");
Object[] args = joinPoint.getArgs();
//修改参数
for (int i = 0; i <args.length ; i++) {
args[i]=args[i]+" update";
}
String thisname = joinPoint.getThis().getClass().getName();
System.out.println(thisname);
String targetName = joinPoint.getTarget().getClass().getName();
System.out.println(targetName);
Signature signature = joinPoint.getSignature();
System.out.println(signature.getName());
try {
joinPoint.proceed(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("around end ");
}

运行结果:

SpringAop环绕通知和JoinPoint and ProceedingJoinPoint_spring


SpringAop环绕通知和JoinPoint and ProceedingJoinPoint_joinpoint_02


举报

相关推荐

0 条评论