SPRING AOP 修改目标方法参数

1. AOP 切面类

package com.sande.test;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AspectTest {
	@Pointcut(
			"execution(* com.sande.test.AspectInterface.printTest(..))" 
			)
	public void aspectPrintTest() {}
	
	//@Around("execution(* com.sande.test.AspectInterface.printTest(..))")
	@Around("aspectPrintTest()")
	public int test1(ProceedingJoinPoint point) throws Throwable {
		int i=0;
		 Object[] args = point.getArgs();
		 for (Object object : args) {
			System.out.println("切面输出:" + object);
			args[i]=(Object)((int)object+10);
		}
		 
		return (int) point.proceed(args);
		
	}
}

 

2. 目标接口

package com.sande.test;

public interface AspectInterface {

	int printTest(int num);
}

 

3. 目标接口实现类

package com.sande.test;

import org.springframework.stereotype.Service;

@Service
public class AspectInterfaceImpl implements AspectInterface {

	@Override
	public int printTest(int num) {
		System.out.println("输出:" + num);
		return num;
	}
}

 

4. 单元测试类

package com.sande.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.sande.user.UserServerApplication;



@RunWith(SpringRunner.class)
@SpringBootTest(classes=UserServerApplication.class)
public class AspectTestMain {
	
	@Resource
	private AspectInterface aspectInterface;
	
	@Test
	public  void printTest() {
		//AspectInterface aspectInterface = new AspectInterfaceImpl();
		int num2 = aspectInterface.printTest(1);
		System.out.println("main  num2:" + num2);
	}
}

5. Bean 配置类

package com.sande.user.config;



import org.apache.tomcat.util.http.LegacyCookieProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sande.test.AspectInterface;
import com.sande.test.AspectInterfaceImpl;
import com.sande.test.AspectTest;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.io.Serializable;

@Configuration

public class BeanAutoConfiguration {


	
    @Bean
    public AspectInterface aspectInterfaceImpl () {
    	return new AspectInterfaceImpl();
    }
    
    @Bean
    public AspectTest aspectTest() {
    	return new AspectTest();
    }


}

 

6. 测试结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值