java 注解

java中元注解有四个: @Retention @Target @Document @Inherited;

   @Retention:注解的保留位置         

      @Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
      @Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
      @Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
  
  @Target:注解的作用目标
        
        @Target(ElementType.TYPE) //接口、类、枚举、注解
        @Target(ElementType.FIELD) //字段、枚举的常量
        @Target(ElementType.METHOD) //方法
        @Target(ElementType.PARAMETER) //方法参数
        @Target(ElementType.CONSTRUCTOR) //构造函数
        @Target(ElementType.LOCAL_VARIABLE)//局部变量
        @Target(ElementType.ANNOTATION_TYPE)//注解
        @Target(ElementType.PACKAGE) ///包

@Document:说明该注解将被包含在javadoc中

  @Inherited:说明子类可以继承父类中的该注解

举例:
   @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD})
    public @interface AnnatDemo{
        public int value();
    }
以上代码定义了@AnnatDemo注解,作用目标是 用于对方法注解,并且保留在运行时的环境中,我们可以利用反射 获得一个方法上的注解 调用定义的方法,

比如@AnnatDemo 作用于以下方法:
public interface IClientProtocolEx extends IProtocol {
  int METHOD_START=0;
  @AnnatDemo(METHOD_START)
   public String say(String person);
}

那么可以利用以下代码进行反射:
Class ipt=IClientProtocalEx.class;
   Method[] mts=ipt.getMethod();
for(Method mt:mts)
   {
    AnnatDemo ad=mt.getAnnotation(AnnatDemo.class);//如果方法上 没有该注解 则返回null
int value=ad.value();
   System.out.println("value:"+value);
   }

注解是用于建设基础jar包的一部分 项目都有自己的框架,若运用恰当,注解则为其中良好的一部分!

----------------------------------------------------------------------------

/**
* * 校验Bean的字段,暂时支持有以下注解的字段的Bean的校验<br/>

*/
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ValidateBean {
}




<bean class="cn.focus.dc.jiajing.interceptors.BeanValidator"/>




package cn.focus.dc.jiajing.interceptors;

import cn.focus.dc.jiajing.annotation.ValidateBean;
import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.ParamValidator;
import net.paoding.rose.web.paramresolver.ParamMetaData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;


public class BeanValidator implements ParamValidator {

private Logger logger = LoggerFactory.getLogger(BeanValidator.class);


@Override
public boolean supports(ParamMetaData metaData) {
return metaData.isAnnotationPresent(ValidateBean.class);
}

@Override
public Object validate(ParamMetaData metaData, Invocation inv, Object target, Errors errors) {
return null;
}


}



@Post("add")
public String add(@ValidateBean JjSpecial jjSpecial,@Param("status") @DefValue("1") String status)
{
try {
int newCreateTime = CalendarUtil.getTimeStamp();

jjSpecial.setCreateTime(newCreateTime);
jjSpecial.setUpdateTime(newCreateTime);
jjSpecial.setStatus(status);

long id = jjSpecialDAO.save(jjSpecial);

return JsonResponse.ok(id);
} catch (Exception e) {
logger.error(e.getMessage(),e);
return JsonResponse.badResult(MsgConstant.SERVER_ERROR);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值