1、新建一个springboot的框架
https://blog.csdn.net/fly7765/article/details/94594920
2、pom.xml中添加redis和切面的依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectj-tools</artifactId>
<version>1.0.6</version>
</dependency>
3、新建一个自定义注解类
Target注解目的为表示该自定义注解可以声明的位置
Retention注解的目的为标注该自定义注解起作用的时间
Documented表示被注解的方法会在javadoc工具记录
package com.cl.test.rediss.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD,ElementType.FIELD})
@Retention(value=RetentionPolicy.RUNTIME)
@Documented
public @interface MyValue {
String value();
}
4、定义一个切面类
Aspect注解表示该类是一个切面类
Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注,从而把普通pojo实例化到spring容器中
Pointcut定义切点,即当其他类或方法或参数(根据该注解的参数进行定义)属于定义的内容的时候,会触发下面的方法
Around环绕方式,在该注解标注的方法中调用触发的方法
package com.cl.test.rediss.config;
import java.util.concurrent.TimeUnit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.stereotype.Component;
import com.cl.test.rediss.services.RedisUtils;
@Aspect
@Component
public class AspectDemo {
public AspectDemo() {
}
@Pointcut("@annotation(com.cl.test.rediss.config.MyValue)")
public void pointCut(){
}
@Around("pointCut()")
public Object Cut(ProceedingJoinPoint point) throws Throwable{
Config config = new Config();
config.useSingleServer().setAddress("127.0.0.1:6379");
RedissonClient client = RedisUtils.getInstance().getRedisson(config);
RLock rlock = client.getLock("cutLock");
rlock.tryLock(1, 1, TimeUnit.SECONDS);
MethodSignature methodSignature = (MethodSignature)point.getSignature();
System.out.println("valuename:"+methodSignature.getMethod().getAnnotation(MyValue.class).value());//获取自定义注解中value的值,可以通过值再次区分做不同的操作
Object obj = point.proceed();
rlock.unlock();
return obj;
}
}
5、写一个被自定义注解MyValue注解的方法
这里写了两个,一个有返回值,一个没有返回值,目的是为了检测切面编程中,针对这两者的处理不会出错
package com.cl.test.rediss.services;
import java.util.concurrent.TimeUnit;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.cl.test.rediss.config.MyValue;
@Service
public class AspectCut {
int i = 1;
@MyValue(value="aspectCut")
public String name (String in) throws InterruptedException{
i = i +1;
System.out.println(i);
TimeUnit.MILLISECONDS.sleep(100);
i = i - 1;
String name = String.valueOf(i);
return name;
}
@MyValue(value="nothing")
public void returnNull(){
System.out.println("return Null");
}
}
5、调用方法
package com.cl.test.rediss.resources;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cl.test.rediss.services.*;
@RestController
public class Resource {
@Autowired
public Resource(){
}
@RequestMapping("/zero")
public void ZeroSys() throws Exception{
String result = aspectCut.name("ki");
System.out.println("return:"+result);
aspectCut.returnNull();
}
}
输出结果:
valuename:aspectCut
2
return:1
valuename:nothing
return Null
其中前三行为有参数的方法输出值,后两项为无参方法的输出结果