• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Safe Publishe issue for a POJO used in Spring

Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm working on a project using Spring2.5 and JBoss, one of my team members created a spring bean class like below:

public class MemberConfig {
static private MemberConfig config;

public static void setConfig(MemberConfig config) {
MemberConfig.config = config;

public static MemberConfig getInstance () {
return config;



and then in ContextLoaderListener.contextInitialized() method this bean is accessed like below:

MemberConfig conf = applicationContext.getBean("memberConfig");
MemberConfig.setConfig(conf); //unsafe publish

I believe the way we access the static variable: MemberConfig.config is not threadsafe, as when MemberConfig.setConfig(conf) is called in ContextLoaderListener.contextInitialized(), no synchronization is employed, it's not a safe publish. So, when we call MemberConfig.getInstance () it might return a stale reference or null.

However my colleague thought since MemberConfig.config is initialized in Spring ContextLoaderListener.contextInitialized(), it would be threadsafe.

I thought my judgement is correct, however I cannot find a way to prove it. when I send multiple concurrent http requests to the JBoss server where the spring application hosted, it seems to be working properly, MemberConfig.getInstance () never returned null, no NullPointerException was thrown.

any ideas about it?

Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not so familiar with Spring, but I think your colleague may have a point. The ContextLoaderListener.contextInitialized() should be called in one thread as the application context is first being initialized, prior to its ability to host requests. This would mean that as a matter of process the config value would have to be set prior to any servlet being called which could access the value.

This may not be guaranteed though, perhaps based on configuration or app server version. For example Springs' documents say: "For Servlet 2.2 containers and Servlet 2.3 ones that do not initalize listeners before servlets, use ContextLoaderServlet. " This at least suggests there are some cases where a Servlet may be loaded prior to the ContextLoaderListener, and therefore could try to access the value before being published.

This case is probably hard to test though. I would try to test it with a servlet which tries to access the value in its init() method rather than one of the request methods. I would also see if making the servlet load pretty early would help. And all that, if it does break the code, may prove not useful if it doesn't match real use-case.
    Bookmark Topic Watch Topic
  • New Topic