/**
 * @author xyf
 * @date 2021/7/7 13:47
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,})
public @interface aopTest {

}
Copy the code

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("spring_test_aop")
public class ApplicationConfigure {
}
Copy the code

@aspect @Component public class config {/** * Around means before+after and there are other ways that we haven't tried here. * Annotation put an @aoptest that marks this Aspect The join point identifies the annotated Method. The join point now stands for the testController's test [with @aopTest] Method */ @Around("@annotation(spring_test_aop.aopTest)") public Object testAop(ProceedingJoinPoint joinPoint) throws Throwable { Rsp rsp = new Rsp(); Object[] objects = joinPoint.getargs (); System.out.println(" join point parameter: "); Stream.of(objects).collect(Collectors.toList()).forEach(System.out::println); // get the first input parameter Req Object reqObject = objects[0]; Req req = (Req) reqObject; ArrayList<String> signFlagList = new ArrayList<>(); ArrayList<String> signValueList = new ArrayList<>(); Field[] fields = Req.class.getDeclaredFields(); For (int I = 0; i < fields.length; i++) { fields[i].setAccessible(true); String fname = fields[i].getName(); FValue = fields[I]. Get (reqObject); fValue = fields[I]. /** * use annotations to indicate the type of the field. SignFlag = fields[I]. GetAnnotation (signflag.class); SignFlag = fields[I]. if (signFlag ! = null) { signFlagList.add(fname+"--"+(fValue == null ? null : fValue.toString())); SignValue SignValue = fields[I]. GetAnnotation (signValue.class); if (signValue ! = null) { signValueList.add(fname+"--"+(fValue == null ? null : fValue.toString())); } } Object result = joinPoint.proceed(); rsp.setMsg( signFlagList.toString() + signValueList.toString() + ((Rsp) result).getMsg()); Return RSP; }}Copy the code

@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Req {
@SignFlag
private  String name;
@SignValue
private  String password;
Copy the code

}


@Data
@NoArgsConstructor
@Accessors(chain = true)
public class Rsp {
private String msg;
}
Copy the code

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SignFlag {
}
Copy the code

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface SignValue {
}
Copy the code

public class test { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ApplicationConfigure.class); System. Out.println (" test returns: "+ applicationContext. GetBean (testController. Class). The test (new to the Req (). The elegantly-named setName (" zhang"). The setPassword (" 123 "))); }}Copy the code

@Component public class testController {@aopTest() public Rsp test(Req Req) {system.out.println (" Controller implementation! ") ); Return new Rsp().setmsg ("controller returns "); }}Copy the code