发现带注释的方法

Discovering annotated methods

在我的 Spring 应用程序中,我有使用 Spring 缓存机制的组件。每个 @Cacheable 注释指定要使用的缓存。我想自动发现启动时需要的所有缓存,以便可以自动配置它们。

最简单的方法似乎是创建一个标记接口(例如:CacheUser)以供每个缓存组件使用:

@Component
public class ComponentA implements CacheUser {
  @Cacheable("dictionaryCache")
  public String getDefinition(String word) {
    ...
  }
}

然后我会 Spring 自动发现此接口的所有实现并将它们自动连接到一个配置列表,该列表可在配置缓存管理器时使用。这行得通。

@Autowired
private Optional<List<CacheUser>> cacheUsers;

我的计划是利用每个发现的 class 并找到所有用 @Cacheable 注释的方法。从那里我可以访问注释的属性并获取缓存名称。我正在使用 AnnotationUtils.findAnnotation() 来获取注释声明。

这就是计划失败的地方。 Spring 实际上连接代理而不是原始组件,并且注释不会复制到代理的方法中。我发现的唯一解决方法是利用代理实现 Advised 提供对代理 class:

的访问这一事实
((Advised)proxy).getTargetSource().getTargetClass().getMethods()

从那里我可以获得原始注释,但这种方法显然很脆弱。

所以两个问题,真的:

  1. 是否有更好的方法来获取代理定义的注释 class?
  2. 您能否建议任何其他方法来发现 @Cacheable 在我的项目中的所有用途?我喜欢没有标记界面。

谢谢!

Spring 有很多基础设施接口,可以帮助您进入容器 and/or beans 的生命周期。为了您的目的,您想要使用 BeanPostProcessor and the SmartInitializingSingleton

BeanPostProcessor会得到所有构造的bean的回调,你只需要实现postProcessAfterInitialization方法。您可以在该方法中检测注释并填充缓存列表。

然后在 SmartInitializingSingletons afterSingletonsInstantiated 方法中,您使用此列表 bootstrap/init 您的缓存。

类似下面的内容(未经测试但应该给您一个想法)。

public class CacheInitialingProcessor implements BeanPostProcessor, SmartInitializingSingleton {

    private final Set<String> caches = new HashSet<String>();

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        Class<?> targetClass = AopUtils.getTargetClass(bean);
        ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                Cacheable cacheable = AnnotationUtils.getAnnotation(method, Cacheable.class);
                if (cacheable != null) {
                    caches.addAll(Arrays.asList(cacheable.cacheNames()));
                }
            }
        });
        return bean;
    }

    @Override
    public void afterSingletonsInstantiated() {
        for (String cache : caches) {
            // inti caches.
        }
    }
}