Skip to content

Injecting prototypes into tests using @Resource appears to be broken [SPR-9580] #14214

Closed

Description

@spring-projects-issues

Tom Ziemer opened SPR-9580 and commented

When injecting a prototype bean into a test derived from AbstractJUnit4SpringContextTests using @Resource, it seems that the bean is only created once.
Prior to Spring 3.1.2 this was not the case, the framework provided a new instance for each test run.

As a workaround, I now use @Autowired + @Qualifier.

The attached project uses a unit test to show the problem. Changing the Spring version to 3.1.1-RELEASE (or lower) in the pom fixes the issue.


Affects: 3.1.2

Attachments:

Issue Links:

5 votes, 6 watchers

Activity

  1. spring-projects-issues commented on Jul 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Juan Osuna commented

    I am getting the same issue with previously working non-test-related production code. Upgrade from 3.1.1 to 3.1.2 seems to be causing several problems with session and prototype scopes.

    I have an bean object tree with the root being session scope. The rest of the tree consists of prototype beans. The same prototype instance is being injected into different beans in the object tree rather than a new one being created for each @Resource field reference. This behavior should be logically impossible by definition.
    Also, I am seeing cases where different session bean instances are being injected into prototype beans, where the session beans should be the same instance. It is almost like the expected behavior of prototype and session scopes has been swapped.

  2. spring-projects-issues commented on Jul 17, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Michał Rembiszewski commented

    Having a very similar behavior:

    I have a session scoped bean A injected into another session scoped bean B with @Resource(name = "beanA"). When I login for the first time, everything works as expected. When I create subsequent sessions, I get a new instance of bean B for each session but they all have the same first instance of bean A injected. It looks as if this dependency was somehow cached in InjectionMetadata for BeanDefinition for bean B. I didn't investigate much farther since we are just trying migration from 3.0 to 3.1 and this was the first thing to come up.

  3. spring-projects-issues commented on Jul 21, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Serg commented

    I have the similar behavior with injectiong prototype into prototype using @Resource even in main application, not to mention tests. So I think it is major problem in spring 3.1.2

  4. spring-projects-issues commented on Jul 25, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dan Tran commented

    Took us a few days to debug this issue after trying to upgrade from 3.1.1 to 3.1.2 for our jsf app

  5. added
    type: bug
    in: core
    on Jan 11, 2019
  6. 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