/*** * 处理注解 * 在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