Announcement Announcement Module
Collapse
No announcement yet.
Advised bean no longer returned via getBeansOfType Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Advised bean no longer returned via getBeansOfType

    I have a singleton bean like this:

    Code:
    @Component
    public class MyBean {
        public void myMethod();
    }
    I retrieve it in several places like this:

    Code:
    listableBeanFactory.getBeansOfType(MyBean.class)
    This works great. (Also it is autowired by type in several places)

    So, now I have advised the myMethod() method using Spring AOP (aspectj).

    Now, when the bean is instantiated, a proxy object is created as expected.

    However, this causes getBeansOfType(MyBean.class) to not return my bean.


    I've traced through Spring's code and found this:

    getBeansOfType eventually calls AbstractBeanFactory.isTypeMatch, which looks like this:


    Code:
    public boolean isTypeMatch(String name, Class targetType) throws NoSuchBeanDefinitionException {
        String beanName = transformedBeanName(name);
        Class typeToMatch = (targetType != null ? targetType : Object.class);
    
        // Check manually registered singletons.
        Object beanInstance = getSingleton(beanName);
        if (beanInstance != null) {
            if (beanInstance instanceof FactoryBean) {
                ... snip ...
            }
            else {
                return !BeanFactoryUtils.isFactoryDereference(name) &&
                        typeToMatch.isAssignableFrom(beanInstance.getClass());
            }
        }
        ... snip ...
    }
    The problem is that typeToMatch.isAssignableFrom(beanInstance.getClass ()); returns false. (MyBean.class.isAssignableFrom(proxyObject.getClas s()) is false)

    Is this a bug, or am I not doing something correctly?

  • #2
    From tracing through the code, I found that I needed to set ProxyConfig.proxyTargetClass to true (which will allow cglib proxies for classes). Now I just have to figure out how/where to set that property

    Comment

    Working...
    X