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

@Resource injection of singleton in prototype using AnnotationConfigApplicationContext is not thread-safe [SPR-9852] #14485

spring-projects-issues opened this issue Oct 3, 2012 · 1 comment
in: core type: bug


Copy link

@spring-projects-issues spring-projects-issues commented Oct 3, 2012

Bob Tiernay opened SPR-9852 and commented

The attached test case demonstrates that @Resource of injection of singleton in prototype using AnnotationConfigApplicationContext is not thread-safe. However, changing @Resource to @Autowired seems to resolve the issue.

Please see for more details

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.pentaho.di.core.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


 * @see
 * @see
 * @author btiernay
@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class)
public class AnnotationCallableConfigTest {

    Callable<Holder> callable;

    public void test() throws InterruptedException, ExecutionException {
        final int threads = 10;
        ExecutorService service = Executors.newFixedThreadPool( threads );

        List<Future<Holder>> futures = new ArrayList<>();
        for ( int i = 0; i < threads; i++ ) {
            futures.add( service.submit( callable ) );

        service.awaitTermination( 1, TimeUnit.MINUTES );

        for ( Future<Holder> future : futures ) {
            final Holder holder = future.get();
            Assert.assertNotNull( holder.value );

    public static class Holder {
        // Changing this to @Autowired makes the test pass
        @Resource(name = "singleton")
        protected Integer value;

    public static class Config {

        public Integer singleton() {
            return 1;

        public Holder prototype() {
            return new Holder();

        public Callable<Holder> function() {
            return new Callable<Holder>() {
                public Holder call() {
                    return prototype();


Affects: 3.1.2

Reference URL:

Issue Links:

  • #14439 Autowired properties can remain unset during concurrent instantiation of prototype-beans
  • #14261 Regression: scoped beans being cached too aggressively
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 4, 2012

Juergen Hoeller commented

This very much looks like a variation of #14439 and seems to be covered by that fix already. In any case, your test consistently passes for me after that fix.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
in: core type: bug
None yet

No branches or pull requests

2 participants