org.springframework.web.method.HandlerMethod:getBean().getClass() 与 getBeanType()

问题描述 投票:0回答:1

我必须将注释附加到控制器类。 这是控制器:

@Tag(name = "Unita Offerta", description = "Rest Controller per unita offerta")
@Controller
@RequestMapping(value = { "/protected/unita-di-offerta" })
@Validated
@ProfiloSelezionato
public class UnitaOffertaController extends BaseRestController{

现在,如果我使用(clazz是注释类)

annotation = method.getBeanType().getAnnotation(clazz);

它工作得很好,但如果我使用

annotation = method.getBean().getClass().getAnnotation(clazz);

我无法获取注释。

阅读文档我知道 .getBeanType() 返回处理程序的类型 而

.getBean().getClass()
返回 bean(作为对象类),然后返回运行时类。

但我希望运行时类更准确,或者与通过处理程序获得的类型大致相同

.getBeanType()

在运行时,调试代码,我可以读到

.getBean().getClass()
回归

.UnitaOffertaController$$EnhancerBySpringCGLIB$$dfd1a00a

同时

.getBeanType()
回归

.UnitaOffertaController.

有什么区别?为什么使用

.getBean().getClass()
我无法获得注释

annotations
1个回答
0
投票

多么好的问题!

当你使用一些额外的后处理器或aop时,Spring可能会增强你的类/bean,因此它会创建一个代理类(可以通过奇怪的名称来识别,就像你正确地注意到的那样):

<package>.UnitaOffertaController$$EnhancerBySpringCGLIB$$dfd1a00a

问题是这个代理类DOESN'T与原始类具有相同的注释,因为它是一个完全不同的类,只是代理原始类。

您可以在这里阅读有关代理的更多信息:在 Spring 框架中使用代理(动态代理)的含义是什么?

希望有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.