在 Spring 上下文中查找方法级自定义注释

2022-09-04 00:54:32

我想找出的只是“Spring beans中所有注释为@Versioned的类/方法”。

我创建了我的自定义注释,如下所示:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Versioned {
    .....
}

当我使用Java反射来查找方法时,这个注释可以完美地工作

for(Method m: obj.getClass().getMethods()){
    if(m.isAnnotationPresent(Versioned.class)){
        .... // Do something
    }

但是当我访问Spring bean并尝试类似的检查时,它不起作用:

public class VersionScanner implements ApplicationContextAware{
    public void setApplicationContext(ApplicationContext applicationContext){
        for(String beanName: applicationContext.getBeanDefinitionNames()){
            for(Method m: applicationContext.getBean(beanName).getClass().getDeclaredMethods()){
                if(m.isAnnotationPresent(Versioned.class){
                    // This is not WORKING as expected for any beans with method annotated
                }
            }
        }
    }
}

实际上,此代码确实可以找到其他注释,例如@RequestMapping。我不确定我的自定义注释做错了什么。


答案 1

通过你的代码,我发现你正在使用Spring AOP和CGLIM代理。因此,您的类(具有注释有的方法)被代理。@Versioned

我已经使用您的代码库测试了此解决方案。

使用以下代码,它应该可以解决您的问题。在代码段下方查找更多选项:

@Configuration
public class VersionScanner implements ApplicationContextAware {

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        for (String beanName : applicationContext.getBeanDefinitionNames()) {
            Object obj = applicationContext.getBean(beanName);
            /*
             * As you are using AOP check for AOP proxying. If you are proxying with Spring CGLIB (not via Spring AOP)
             * Use org.springframework.cglib.proxy.Proxy#isProxyClass to detect proxy If you are proxying using JDK
             * Proxy use java.lang.reflect.Proxy#isProxyClass
             */
            Class<?> objClz = obj.getClass();
            if (org.springframework.aop.support.AopUtils.isAopProxy(obj)) {

                objClz = org.springframework.aop.support.AopUtils.getTargetClass(obj);
            }

            for (Method m : objClz.getDeclaredMethods()) {
                if (m.isAnnotationPresent(Versioned.class)) {
                    //Should give you expected results
                }
            }
        }
    }
}

检测代理类:

  • 对于使用任何代理机制的Spring AOP代理,请使用org.springframework.aop.support.AopUtils#isAoPProxy
  • 如果您使用Spring CGLIB代理(不是通过Spring AOP),请使用org.springframework.cglib.proxy.Proxy#isProxyClass
  • 如果使用 JDK 代理进行代理,请使用java.lang.reflect.Proxy#isProxyClass

我刚刚写了一个条件,这在你的情况下就足够了;但是,如果使用多个代理实用程序,则必须根据上述信息编写多个条件。ifif-else


答案 2

applicationContext.getBean(beanName).getClass()为您提供了Spring围绕目标类创建的代理类。

你想要的是从你的Spring Bean中获得目标类(如果有的话)。

Spring提供了一个很好的实用程序类来解决这个问题,称为AopUtils.class。

以下是您将如何使用它:

for(String beanName: applicationContext.getBeanDefinitionNames()){
    Method[] methods = AopUtils.getTargetClass(applicationContext.getBean(beanName)).getDeclaredMethods();

    for(Method m: methods){
        if(m.isAnnotationPresent(Versioned.class){
        }
    }
}

请注意,您必须导入 Maven 依赖项才能获得 AopUtils 类:spring-aop

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring.version}</version>
</dependency>

推荐