Spring and Java visibility

208 Views Asked by At

Lately our system has been experiencing all kinds of Spring related 'voodoo'. It usually goes something like this:

Various singleton beans have (ctor) autowired properties, based on which they instantiate some internal data structures. At some point we began seeing phenomena where the internal state was not initialized, despite the fact that the bean was constructed and handed out to the application. and it happened in a non deterministic manner of course.

A typical bean would look like this:

class MyBean{
   Map<String, Objecet> state;

   @Autowired
   public MyBean(List<Stuff> stuff){
      state = new HashMap<>();
      pupolateStateBasedOnStuff();
   }

   getSomeState(String key){
       // I would get an NPE (state = null) or an instantiated 
       // state that is seemingly empty! 
       state.get(key); 
   }

}

At some point I stumbled on this article and this SO answer and this writing by Brian Goetz. I am suffering from this very problem. After reading these I have two follow up questions:

Goats writes:

So, you can have a final pointer to an array and not have to worry about other threads seeing the correct values for the array reference, but incorrect values for the contents of the array

Question 1

Is this statement transitive? Meaning that this applies not only to the objects referenced by the final variable, but also to the objects they reference etc (covering the entire 'reachable' object tree) ?

Also, what if I have the following scenario:

class Bar{
     volatile Foo foo;

     public Bar() { } 

     // Bar is actually initialized outside of its ctor.
     public init(Object state) {
       foo = new Foo(state);
     }

     getSomeStuff(){
        // will 'stuff' be fully visible because it 
        // was created by assignment to a final variable? 
        return foo.stuff;
     }

}

Foo Looks like this:

class Foo{
   final Object stuff ;

   Foo(Object state){
      this.stuff = computeStuff(state);
   }
}

Question 2

Does this 'trick' ensure the visibility of 'stuff'?

In other words - I have a class that is initialized outside of its ctor (Can't have a final member). But I still want to ensure visibility. So I wrapped the state in an object that assigns it to a final variable inside its ctor.

0

There are 0 best solutions below