How can I share a static field between 2 class loaders?
posted 5 years ago
I've been googling for 2 days and it now seems I'm not understanding something because nobody seems to have my problem. Please, somebody tell me if I'm crazy.
The system's architecture:
I've got a web application running in a SunOne server. The app uses Struts for the MVC part and Spring to deal with business services and DAOs.
Beside the web app, beyond the application context, but in the same physical server, there are some processes, kind of batch processes that update tables and that kind of stuff, that run once a day. Theese processes are plain Java classes, with a main method, that are executed from ".sh" scripts with the "java" command.
What do I need to do?
"Simple". I need one of those Java processes to use one of the web app's service. This service has some DAOs injected by Spring. And the service itself is a bean defined in the Spring configuration file.
The solution is made-up of 2 parts:
1. I created a class, in the web app, with a static method that returns any bean defined in the Spring configuration file, or in other words, any bean in the application context. In my case, this method returns the service I need.
The ApplicationContext is injected to the class by Spring through the setApplicationContext method. This is set in the Spring configuration file.
Well, this works fine if I call the getBean method from any class in the web app. But that's not what I need. I need to get a bean from outside the web app. From the "Java batch process".
2. Why doesn't it work from outside the web app? Because when I call getBean from the process outside the web app, a different class loader is executed to load the SpringApplicationContext class. Thus, the static field appContext is null. Am I right?
So, the question I need you to please answer me, the question I didn't find in Google:
How can I share the static field between the 2 class loaders?
If I can't, how can I load the SpringApplicationContext class, from the "Java batch process", with the same class loader my web app was started?
Or, do I need to load the SpringApplicationContext class again? Can't I use, from the process, the class already loaded by my web app?