Skip to content

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

Closed

Description

@spring-projects-issues

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:

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

Activity

  1. spring-projects-issues commented on Dec 25, 2016

    @spring-projects-issues
    CollaboratorAuthor

    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.

  2. added this to the 4.3.6 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions