Back Forum Reply New

AppContext Injection Threading Issues

Hi,

I have an application context that I'm loading and it looks like I'm having some issues with what seems to be cross-thread visibility.

I'm starting a ClassPathXmlApplicationContext with only my .xml configs as my arguments.

Now I'm creating 3 objects.

A, B, and C.

A is a runnable. B is a bean which (somewhere down the line) gets accessed by the thread A creates. C is a resource which B needs.

A's runnable is created and started by another bean's @PostConstruct method.

So I debugged and I see C getting injected into B, then the A runnable gets run. Unfortunately when A's thread reaches B.C... C is still null, even though I confirmed it was not null after getting set by the application context (thus my assumption that this is a visibility issue across threads).

I swear I read about some setting to control ApplicationContext synchronization before but I can't seem to find anything to help me and it would make very little sense (hack hack hack) for me to make B's reference to C volatile since B is not thread safe to begin with. Of course if there is no Spring based solution I suppose it will have to be thread safe and require redesigning at which point that won't be a hack... but I would rather leave it unsafe.

Ideas?

(I am considering trying Spring 3 and doing my own synchronization in my AppConfig class, is this an appropriate solution? )

Okay, more simple question.

Is it wrong to create threads within init-method (@PostCreate) methods?

Should I only create new threads after my ApplicationContext constructor returns?

I still have issues with random beans not having their properties set when my init-method originating threads reach them.

It seems that init() is actually getting called before all my beans are even created... so this isn't even a threading issue it's simply how the code works I suppose, bad use of init-method on my part?
¥
Back Forum Reply New