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. 测试结果