注解方式修改方法的入参 (注解参数为类)

需要引入的pom依赖:


     org.aspectj
     aspectjweaver
     1.9.1

1.定义注解

public interface Convert {

    /**
     * 返回结果转换
     *
     * @param t
     * @return
     */
    T convert(T t);
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Input {

    Class value();
}

2.设置Aop拦截

@Aspect
@Component
public class ConvertAspect implements ApplicationContextAware {

    private ApplicationContext context;

    @Before(value = "@annotation(input) && args(query)")
    public void beforeMethod(JoinPoint point, Input input, Query query) {
        Convert convert = context.getBean(input.value());
        convert.convert(query);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}

3.定义注解参数类

@Component
public class VehicleQueryConvert implements Convert {

    private static final Integer QUERY_LIMIT = 1000;

    @Autowired
    private VehicleService vehicleService;

    @Override
    public VehicleQuery convert(VehicleQuery vehicleQuery) {

        return plateConvert(vehicleQuery);
    }
}

4.注解的使用

@Input(VehicleQueryConvert.class)
 public Page list(VehicleQuery query) {
     return null;
 }

你可能感兴趣的:(java,注解)