Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getBeanNamesForType(ResolvableType) does not reliably work for beans with AOP proxies [SPR-14097] #18669

Closed
spring-issuemaster opened this issue Mar 30, 2016 · 1 comment

Comments

@spring-issuemaster
Copy link
Collaborator

commented Mar 30, 2016

David J. M. Karlsen opened SPR-14097 and commented

Relates to #16761

When beans are instrumented with interceptors it doesn't resolve them properly.
E.g. this returns me 0 candidates:

ResolvableType resolvableType = ResolvableType.forClassWithGenerics( StgBusinessService.class, function.getClass() );

while this works

private <T extends Function> StgBusinessService<T> findBusinessService( T t ) {
        Map<String,StgBusinessService> services = applicationContext.getBeansOfType( StgBusinessService.class );
        ParameterizedType parameterizedType = TypeUtils.parameterize( StgBusinessService.class, function.getClass() );

        return services
            .values()
            .stream()
            .filter( service -> ArrayUtils.contains( AopUtils.getTargetClass( service ).getGenericInterfaces(), parameterizedType ) )
            .findFirst()
            .orElseThrow( () -> new IllegalStateException( "No handler for function type: " + function ) );
}

The 1st variant passes in my simple unit-test where there are no application of AOP, while it doesn't when loading the full-blown application which adds AOP.


Affects: 4.2.5

Issue Links:

  • #16761 Add generics / parameterized type support to ListableBeanFactory getBeanNamesForType/getBeansOfType methods
  • #22056 getBeanNamesForType(ResolvableType) doesn't work for raw singleton instance from @Bean method with generic return type
  • #18690 ListableBeanFactory#getBeanNamesForType(ResolvableType) fails to return generic @Bean methods
  • #19578 getBeanNamesForType(ResolvableType) doesn't match generic factory method return type for yet-to-be-created bean

Referenced from: commits f805427, 9a39a25, 3b7ca7e, edea66a

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 25, 2016

Juergen Hoeller commented

The recent fix for #19578 addressed the problem for type prediction against factory methods, also behind proxies. Type prediction also works for AOP proxies now, but for already instantiated singleton objects, only CGLIB proxies are correctly matched in terms of generics.

For interface-based JDK proxies, we need to explicitly look at the target type of the bean if the proxy type doesn't generically match. At the BeanFactory level, we typically do that through checking resolved types on the underlying bean definition, which also does the job here. This is largely equivalent to an AOP target class check but does not require Spring AOP specifics and therefore no spring-aop dependency; as a bonus, it also works with custom JDK proxies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.