本文共 2558 字,大约阅读时间需要 8 分钟。
当SOAService bean通过两个BeanPostProcessor处理时,动态代理后发现bean上的注解@Service丢失。这一现象通常发生在开启CGLIB代理模式时。
在Spring Boot应用中,@EnableAspectJAutoProxy(proxyTargetClass = true)注解用于启用AspectJ自动代理。proxyTargetClass = true的作用是将目标类直接作为代理类,这样在代理过程中会丢失原始类的注解信息,导致自定义注解无法保留。
要解决@Service注解丢失的问题,可以采取以下方法:
在自定义注解上添加@Inherited
@Service上添加@Inherited注解,可以使得动态代理支持继承注解,从而保留注解信息。调整项目接口层
对于第三方提供的接口,建议在项目接口层进行适当的调整,确保动态代理不会丢失重要的注解信息。动态修改注解以支持继承
通过自定义BeanPostProcessor动态修改注解,使其支持继承。以下是具体实现代码:public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Service anon = bean.getClass().getAnnotation(Service.class); if (anon != null) { try { InvocationHandler h = Proxy.getInvocationHandler(anon); // 设置@Service注解支持继承 Field typeField = h.getClass().getDeclaredField("type"); typeField.setAccessible(true); Field annotationTypeField = Class.class.getDeclaredField("annotationType"); annotationTypeField.setAccessible(true); AnnotationType annotationType = (AnnotationType) annotationTypeField.get(typeField.get(h)); Field inheritedField = AnnotationType.class.getDeclaredField("inherited"); this.updateFinalModifiers(inheritedField); inheritedField.set(annotationType, true); // 获取 AnnotationInvocationHandler 的 memberValues 字段 Field memberValuesField = h.getClass().getDeclaredField("memberValues"); // 因为这个字段是 private final 修饰,所以要打开权限 memberValuesField.setAccessible(true); // 获取 memberValues Map memberValues = (Map) memberValuesField.get(h); Service service = Stream.of(bean.getClass().getInterfaces()) .filter(iface -> iface.getAnnotation(Service.class) != null) .collect(Collectors.toList()) .get(0) .getAnnotation(Service.class); memberValues.put("version", service.version()); memberValues.put("group", service.group()); } catch (Exception e) { throw new BeanCreationException(String.format("%s %s %s %s %s" , "修改" , ClassUtils.getQualifiedName(bean.getClass()) , "的注解" , ClassUtils.getQualifiedName(Service.class) , "的 group值和version值出错") , e); } } return bean;} 通过上述方法,可以有效地解决动态代理后注解丢失的问题,确保自定义注解信息在代理类中得到保留。
转载地址:http://xseuz.baihongyu.com/