SpringAOP:目标类如何向切面类中传参

5 篇文章 0 订阅

如何向一个切面类进行传参?
切面类

package com.xbz.learn.spring.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 切面类
 * @author xubaozhong
 * 
 */
@Aspect
public class FirstAspect {
    @Pointcut("execution (* com.xbz.learn.spring.aspect.FirstAspectTarget.*(..))")
    public void pointcut(){

    }

    @Before("execution (* com.xbz.learn.spring.aspect.FirstAspectTarget.doService(User))&&args(user)")
    public void doBefore(User user){
        System.out.println(user);
        System.out.println("doBefore...modify parameter");
        user.setUsername("Before");
    }

    @After("execution (* com.xbz.learn.spring.aspect.FirstAspectTarget.doService(User))&&args(user)")
    public void doAfter(User user){
        System.out.println("After-->"+user);
        System.out.println("doAfter...modify parameter");
        user.setUsername("After");
    }

    @AfterReturning("execution (* com.xbz.learn.spring.aspect.FirstAspectTarget.doService(User))&&args(user)")
    public void doAfterReturning(User user){
        System.out.println("AfterReturning-->"+user);
        System.out.println("AfterReturning...modify parameter");
        user.setUsername("AfterReturning");
    }
}

目标类

package com.xbz.learn.spring.aspect;

import org.springframework.stereotype.Component;

/**
 * 切面目标类,被拦截的目标类
 * @author xubaozhong
 *
 */
@Component
public class FirstAspectTarget {

    /*public void doService(){
        System.out.println("doService----处理业务逻辑");
        throw new RuntimeException(new Exception("业务报错"));
    }*/

    public void doService(User user){
        System.out.println(user);
    }
}

JavaConfig

package com.xbz.learn.spring.aspect;

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

@Configuration
//@ComponentScan
@EnableAspectJAutoProxy
public class FirstAspecJavaConfig {

    @Bean
    public FirstAspect getFirstAspect(){
        return new FirstAspect();
    } 

    @Bean
    public FirstAspectTarget getFirstAspectTarget(){
        return new FirstAspectTarget();
    } 
}

Test类

package com.xbz.learn.spring.aspect;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={FirstAspecJavaConfig.class})
public class FirstAspectTest {
    @Autowired
    private FirstAspectTarget target;
    @Test
    public void testFirstAspect(){
        User user = new User();
        user.setUsername("");
        target.doService(user);
        System.out.println("finally-->"+user);
    }
}

class User{
    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + "]";
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    private String username;
    private String password;

}
输出结果:
User [username=, password=null]
doBefore...modify parameter
User [username=Before, password=null]
After-->User [username=Before, password=null]
doAfter...modify parameter
AfterReturning-->User [username=After, password=null]
AfterReturning...modify parameter
finally-->User [username=AfterReturning, password=null]

可见Before | After | AfterReturning的顺序。
注意After无论方法是否成功执行,均会调用

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值