java无效的参数_java自定义参数注解@Target(ElementType.PARAMETER)无效?

我自定义了一个参数注解@NotBlank, Target为(ElementType.PARAMETER) .

注解到方法的入参上, Aspect处理.

但是运行时,发现注解没生效,没进Aspect代码断点. 请大侠指点下.

代码如下:

注解定义

package com.xxx.common.util;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* FileName: NotBlank

* Description:

* Version: v1.0.0

* Author: liutf

* Date: 2017-02-08

*/

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.PARAMETER)

public @interface NotBlank {

String value() default "";

}

注解使用

package com.xxx.service.impl;

import com.xxx.common.util.NotBlank;

import com.xxx.dao.ProductDefDAO;

import com.xxx.entity.ProductDefEntity;

import com.xxx.service.ProductDefService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import static com.xxx.common.constant.RigConst.DATA_STATE.VALID;

@Service

@Transactional

@Slf4j

public class ProductDefServiceImpl implements ProductDefService{

@Autowired

private ProductDefDAO dao;

@Override

@Cacheable(value="exProdCache")

public ProductDefEntity getProduct(@NotBlank String channel) {

ProductDefEntity entity = dao.findByChannelAndState(channel, VALID);

if (entity == null) {

log.error("渠道产品为空:channel:{}",channel);

}

return entity;

}

}

注解Aspect处理

package com.xxx.common.util;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.reflect.MethodSignature;

import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**

* FileName: NotBlankAspectHandler

* Description:

* Version: v1.0.0

* Author: liutf

* Date: 2017-02-08

*/

@Component

@Aspect

public class NotBlankAspectHandler {

@Around("@annotation(NotBlank)")

public Object validateAround(ProceedingJoinPoint joinPoint) throws Throwable {

boolean result = false;

MethodSignature signature = (MethodSignature) joinPoint.getSignature();

Method method = signature.getMethod(); //得到拦截的方法

Object[] args = joinPoint.getArgs(); //方法的参数

for (Object arg : args) {

if (arg instanceof String){

Precondition.checkStringArgument((String) arg);

}

}

System.out.println("校验完成..");

return joinPoint.proceed();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值