简体   繁体   English

Spring:如何获取bean层次结构?

[英]Spring: How to get the bean hierarchy?

Is it possible to get the beans in which a bean was injected(Via Spring Framework)? 是否有可能获得注入bean的bean(通过Spring Framework)? And if yes how? 如果是的话怎么样?

Thanks! 谢谢! Patrick 帕特里克

如果您正在寻找合作bean,您可以尝试实现BeanFactoryAware

只是为了扩展David的答案 - 一旦实现了BeanFactoryAware,就会得到BeanFactory的引用,你可以通过BeanFactory.ContainsBean(String beanName)来查询bean工厂中是否存在特定bean

Here's a BeanFactoryPostProcessor sample implementation that may help you here: 这是一个BeanFactoryPostProcessor示例实现,可以帮助您:

class CollaboratorsFinder implements BeanFactoryPostProcessor {

    private final Object bean;
    private final Set<String> collaborators = new HashSet<String>();

    CollaboratorsFinder(Object bean) {
        if (bean == null) {
            throw new IllegalArgumentException("Must pass a non-null bean");
        }
        this.bean = bean;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        for (String beanName : BeanFactoryUtils.beanNamesIncludingAncestors(beanFactory)) {
            BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
            if (beanDefinition.isAbstract()) {
                continue;   // assuming you're not interested in abstract beans
            }
            // if you know that your bean will only be injected via some setMyBean setter:
            MutablePropertyValues values = beanDefinition.getPropertyValues();
            PropertyValue myBeanValue = values.getPropertyValue("myBean");
            if (myBeanValue == null) {
                continue;
            }
            if (bean == myBeanValue.getValue()) {
                collaborators.add(beanName);
            }
            // if you're not sure the same property name will be used, you need to
            // iterate through the .getPropertyValues and look for the one you're
            // interested in.

            // you can also check the constructor arguments passed:
            ConstructorArgumentValues constructorArgs = beanDefinition.getConstructorArgumentValues();
            // ... check what has been passed here 

        }

    }

    public Set<String> getCollaborators() {
        return collaborators;
    }
}

Of course, there's a lot more to it (if you want to also catch proxies of your original bean or whatever). 当然,还有更多内容(如果你还要捕获原始bean的代理或其他)。 And, of course, the above code is completely untested. 当然,上面的代码完全没有经过测试。

EDIT: To make use of this, you need to declare it as a bean in your application context. 编辑:要使用它,您需要在应用程序上下文中将其声明为bean。 As you already noticed, it requires your bean (the one you want to monitor) to be injected into it (as a constructor-arg). 正如您已经注意到的那样,它需要将bean(您要监视的bean)注入其中(作为构造函数arg)。

As your question refers to the "bean hiearchy", I edited to look for the bean names in the entire hierarcy ...IncludingAncestors . 当你的问题提到“bean hiearchy”时,我编辑了在整个层次中寻找bean的名字...IncludingAncestors祖先。 Also, I assumed your bean is a singleton and that it is possible to inject it into the postprocessor (although in theory the postProcessor should be initialized before other beans -- need to see if this actually works). 此外,我假设你的bean是一个单独的,并且可以将它注入后处理器(虽然理论上postProcessor应该在其他bean之前初始化 - 需要查看它是否真的有用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM