Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8623033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:11:23+00:00 2026-06-12T07:11:23+00:00

Is the following test method thread-safe, assuming that this is being called from multiple

  • 0

Is the following test method thread-safe, assuming that this is being called from multiple threads? Sometimes the println statement displays “null”.

The idea is to return a map that will create beans on demand based on a supplied identifier. Note that this is just a simple example to illustrate a similar real life scenario in which one bean did not have its dependencies satisfied (e.g., value.x being null) using the same approach. For bonus points, is there another (better) way to achieve the same effect?

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
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;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import com.oanda.bi.rm.test.AnnotationConfigTest.Config;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class)
public class AnnotationConfigTest {

    @Resource
    Map<String, Value> map;

    @Test
    public void test() throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool( 10 );

        for ( int i = 0; i < 10; i++ ) {
            service.execute( new Runnable() {
                @Override
                public void run() {
                    // Is this thread-safe?
                    Value value = map.get( "value" );

                    // Sometimes null!
                    System.out.println( value.x );
                }
            } );
        }

        service.shutdown();
        service.awaitTermination( 1, TimeUnit.MINUTES );
    }

    public static class Value {
        @Resource
        protected Integer x;
    }

    @Configuration
    public static class Config {

        @Bean
        public Integer x() {
            return 1;
        }

        @Bean
        @Scope("prototype")
        public Value value() {
            return new Value();
        }

        @Bean
        @SuppressWarnings("serial")
        public Map<String, Value> map() {
            // Return a Spring agnostic "bean factory" map
            return Collections.unmodifiableMap( new HashMap<String, Value>() {
                @Override
                public Value get( Object obj ) {
                    String key = (String) obj;

                    if ( key.equals( "value" ) ) {
                        // Create new bean on demand
                        return value();
                    }

                    // Assume other similar branches here...

                    return null;
                }
            } );
        }

    }
}

Update

Given the insightful feedback from Biju Kunjummen, I’ve tried a different approach that uses the application context directly, but it still fails will nulls. This time I’m using a Function abstraction instead of a Map which seems more appropriate:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
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;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import com.google.common.base.Function;
import com.oanda.bi.rm.test.AnnotationFunctionConfigTest.Config;

/**
 * Unit test that tries to perform the same pattern as {#link ResourceConfig} and ensure
 * thread safety.
 * 
 * @see http://stackoverflow.com/questions/12700239/thread-safety-of-calling-bean-methods-from-returned-annonymous-inner-classes/12700284#comment17146235_12700284
 * @see http://forum.springsource.org/showthread.php?130731-Thread-safety-of-calling-Bean-methods-from-returned-annonymous-inner-classes&p=426403#post426403
 * @author btiernay
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class)
public class AnnotationFunctionConfigTest {

    @Resource
    Function<String, Value> function;

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

        for ( int i = 0; i < threads; i++ ) {
            service.execute( new Runnable() {
                @Override
                public void run() {
                    Value value = function.apply( "value" );
                    Assert.assertNotNull( value.x );
                }
            } );
        }

        service.shutdown();
        service.awaitTermination( 1, TimeUnit.MINUTES );
    }

    public static class Value {
        @Resource
        protected Integer x;
    }

    @Configuration
    public static class Config {

        @Bean
        public Integer x() {
            return 1;
        }

        @Bean
        @Scope("prototype")
        public Value value() {
            return new Value();
        }

        @Bean
        public Function<String, Value> function() {
            // Return a Spring agnostic "bean factory" function
            return new Function<String, Value>() {
                @Autowired
                private ApplicationContext applicationContext;

                @Override
                public Value apply( String key ) {
                    if ( key.equals( "value" ) ) {
                        // Create new bean on demand
                        return applicationContext.getBean( key, Value.class );
                    }

                    // Assume other similar branches here...

                    return null;
                }
            };
        }

    }
}

Anyone care to comment why this still appears to be unsafe?

Update

This looks like it might be a Spring bug. I submitted a jira ticket:

https://jira.springsource.org/browse/SPR-9852

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:11:24+00:00Added an answer on June 12, 2026 at 7:11 am

    I wouldn’t recommend the way you have implemented it:

    1. Like Jordan said, the map is not really required there, you are not using it as a hashmap at all, instead just using it to call .value() method.

    2. Spring @Configuration mechanism is being bypassed, internally Spring creates a CGLIB proxy for @Configuration classes and using this knows which dependencies need to be injected where and to create instances which know how to manage scopes, by bypassing it you are essentially not using Spring to manage your bean instances anymore.

    The following does something similar to what you have implemented but I think is simpler and works consistently every time – this is using by using the application context to get a prototype bean and hiding this behind a custom factory this way:

    Updated Implementation

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    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;
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class AnnotationConfigTest {
    
    
        @Autowired PrototypeBeanFactory prototypeFactory;
    
        @Test
        public void test() throws InterruptedException {
            ExecutorService service = Executors.newFixedThreadPool( 10 );
    
            for ( int i = 0; i < 10; i++ ) {
                service.execute( new Runnable() {
                    @Override
                    public void run() {
                        Value value = prototypeFactory.getBean("value", Value.class);
                        System.out.println( "value1.x = "  + value.getX() );
                    }
                } );
            }
    
            service.shutdown();
            service.awaitTermination( 1, TimeUnit.MINUTES );
        }
    
        public static class Value {
            @Autowired
            private Integer x;
    
            public Integer getX() {
                return x;
            }
    
            public void setX(Integer x) {
                this.x = x;
            }
        }
    
    
    
        @Configuration
        public static class Config {
    
            @Bean
            public Integer x() {
                return 1;
            }
    
            @Bean
            @Scope(value="prototype")
            public Value value() {
                return new Value();
            }
    
            @Bean
            public PrototypeBeanFactory prototypeFactory(){
                return new PrototypeBeanFactory();
            }
    
        }
    
    
        public static class PrototypeBeanFactory implements ApplicationContextAware{
            private ApplicationContext applicationContext;
            @Override
            public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
                this.applicationContext = applicationContext;
            }
    
            public<T> T getBean(String name, Class<T> clazz){
                return this.applicationContext.getBean(name, clazz);
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a unit test for a method that contains the following line: String
Following on from one of the answers in this thread; Using XQuery in Linq
I have following spec to test controller method: context #create do it should redirect
I can typically test a regular Test::Unit method using the following commandline syntax for
I have the following C# method: private string[] test() { } I would like
I have the following method where I want to test the event.status property only
I am getting following error: COM object that has been separated from its underlying
is the following method likely to cause a thread to hang which in turn
I am using Reflections to obtain a method from a class that is annotated
I have got the following code in a file called test.java which is located

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.