java注解草稿

/***
 * 处理注解
 * 在class字节中,会记录该类或者该类的方法上的注解
 *
 * 一般注解的处理,不存在代理【动、静】形式
 */
@Test
public void annotacationResovle(){

    Map<String,Object> map = context.getBeansWithAnnotation(Service.class);

    UserServerImp u = (UserServerImp)map.get("testUserServerImp");

    if(!Objects.isNull(u)){
        Service rm = u.getClass().getAnnotation(Service.class);
        if(!Objects.isNull(rm)){
            System.out.println("testUserServerImp----value:"+rm.value().toString());
        }
        for (Method m : u.getClass().getMethods()) {
            //此时的m获取自身的注解时,一定要注意,不可以获取m的原始class然后在获取注解
            //需要通过m实例的直接方法,获取指定类型的注解
            RequestMapping rh = m.getAnnotation(RequestMapping.class);
            String[] v3 = {};
            if(rh != null){
                v3 = rh.value();
                if (v3 != null && v3.length>0) {
                    System.out.println("child----value:"+v3[0].toString());
                }
            }
        }
    }

}


/***
 * 如果获取到动态代理类中的原始注解
 */
@Test
public void cligAnno() {
    Map<String, Object> map = context.getBeansWithAnnotation(Service.class);
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if(!Objects.isNull(value)){
            System.out.println("service的value:"+value.getClass().getSuperclass().getName());
            Service s = value.getClass().getSuperclass().getAnnotation(Service.class);
            if(!Objects.isNull(s)){
                System.out.println("service的value:"+s.value());
            }
            Method[] ms = value.getClass().getSuperclass().getMethods();

            for(Method m : ms){
                System.out.println("Method:name:"+m.getName()+",");
                UserAnnotion t =  m.getAnnotation(UserAnnotion.class);  //自定义注解
                //由于该注解,导致了引用该注解的类会被生成一个代理类,即在spring容器中存放的是一个代理类
                Transactional t1 =  m.getAnnotation(Transactional.class);  //spring事务注解
                if(t != null){
                    System.out.print("childe::"+t.value()+",");
                    System.out.print("childe::"+t.readOnly()+",");
                    System.out.print("childe::"+t.timeout());
                    System.out.println("");
                }
                if(t1 != null){
                    System.out.print("childe::"+t1.value()+",");
                    System.out.print("childe::"+t1.readOnly()+",");
                    System.out.print("childe::"+t1.timeout());
                    System.out.println("");
                }
            }
        }
    }
}

 

 

代理原理:https://www.cnblogs.com/qinggege/p/5288182.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值