Skip to content

Regression: scoped beans being cached too aggressively [SPR-9627] #14261

Closed

Description

@spring-projects-issues

Samuel Bernet opened SPR-9627 and commented

The bugfix #13814 "Scoped-proxy memory leak w/ @Resource injection" leads to bean instances being cached too eagerly in CommonAnnotationBeanPostProcessor. Besides the (desired) caching of AOP scoped-proxies to prevent the memory leak the current code also caches custom scoped bean instances that should not be cached. This happens for example if client code uses AutowireCapableBeanFactory#autowireBean(Object) to inject dependencies (annotation-based) into a manually created bean. In this case no AOP proxies are involved but the cached instances are still used. The underlying scope is never consulted and can thus never signalize that a new instance is to be created and injected because the context of the scope (e.g. request, session) is different from the first call which resulted in the cached instance.

I attached a Test-Case (JUnit 4 Test, requires JUnit and Spring to run). The test-case illustrates the breaking change:

I understand this calling pattern is not very common but nevertheless the affected API is public and our framework relies on this to provide service beans to instances of "visual" classes (a visual models a single page in a web application). This basically renders custom scoped beans (also being used heavily) unusable for us in releases > 3.1.1, thus my classification as "major".

Best regards
Samuel Bernet
MSc ETH Software Engineering
[email protected]


Affects: 3.1.2, 3.2 M1

Attachments:

Issue Links:

Referenced from: commits 04af54a, 26ee0c4, 68c5f20, 1971870

1 votes, 5 watchers

Activity

  1. spring-projects-issues commented on Aug 14, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Wouter Coekaerts commented

    Also see the test case attached to #14318: it shows it happens for prototype scoped beans injected with @Resource, but not with @Autowired.

  2. spring-projects-issues commented on Aug 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    See also the reproduction project attached to duplicate issue #14318

  3. added
    type: bug
    in: core
    on Jan 11, 2019
  4. added this to the 3.1.3 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

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions