I have been using Spring DI and IOC extensively in my earlier projects. As per a new project requirement, we have to setup our projects as follows :
Business project (Java project)
Common project ( Java project)
Presentation project (Web project)
Business and presentation communicates/uses the common layer. However, the business and presentation are to be deployed in different app servers, and the communication between business and presentation is not http based. Presentation invokes the business through iiop protocol. That means, the protocol switching happens, while calling business (Java project) from presentation (web project).
In Business project, I am planning to encapsulate all business logic methods, and DAO methods in a facade object, using SPRING dependency Injection feature and normal OOPS technique. Then, I can expose the facade object using EJB, from the presentation layer, I invoke the ejb and get the facade object from business layer.
My only concern is, where to load the spring context xml, in the business layer ? In the presentation layer, I am pretty sure of the loading, where I can use web.xml and my base action class (in struts) to load the spring context xml using WebapplicationContext. However, in the business layer, which class is the best place where I can invoke/load my spring context and initialize the Spring IOC container ? Is there any provision that can be applied in the app server level ? (currently I am using Web sphere app server) Any help in this topic is highly appreciated.
Since the business & presentation/web layer is not co-located, they will need to use seperate Spring context. Even if they are co-located it is recommeded to keep the Spring context for business & presentation layer seperate as these two layers are different.
Joined: Jun 22, 2009
Thanks for the reply ..
Yes, you are right, there should be separate spring context for business and presentation.
But, I need to know,
if I have a spring context in the business and I want to prepare a business facade object, by injecting all dependencies into the same. Then I need to expose the facade using EJB. Now my concern is, which class in the business layer will be responsible to load/initialize the spring context ?
Ex- Whenever from the presentation layer, I invoke/give a call to the EJB and dig into the business layer, then which class is the best one to load the spring context ? is it a good idea, to load it in the create method of the EJB itself ? I am a bit confused about choosing the best optimal approach, kindly suggest.
It is normally recommended to co-locate web & business layer to avoid overhead of remote communication. You should have a good reason for running web & business layer in different JVM's.
Normally in a web/app server environment, the Spring application context is loaded at startup by using a standard servlet listener. Have a look at ContextLoaderListener.
In case you are using EJB in the business layer then using Spring also sounds like a bit of a overkill to me. In case you are just using EJB as you want to run business & web layer in separate JVM's then as mentioned above you need to have a very good reason for keeping them separate as it adds overhead in terms of performance, maintainability & manageability.
Joined: Jun 22, 2009
We need to run them in 2 different JVM's because of the following reasons :
1. The business layer needs to be the common layer to be used, not only for the web layer, but also from some other applications. Because of security restrictions, they are to be accessed this way only.
2. As per organization std, the protocol switching is a requirement. The web project is to be invoked by the users, which internally calls/invokes the business layer by changing the protocol from http to iiop. In this way, the business layer is more secure and can't be accessible by http.
3. In order to give remote call from presentation to business, we have options like RMI and EJB (Web services are also ruled out by the architectural committee ). But as EJB is considered as much better as compared to RMI approach, hence the same has been chosen.
In my previous projects, I have used Spring and chosen the same to be loaded in the start-up using ContextLoaderListener only. However in the current scenario, I was just evaluating, if Spring fits into the above mentioned conditions and can be loaded in the business layer. If it can be loaded (just as the way it gets loaded at the start-up in the web project), it can serve lots of difficulties and provide dependency injections into the dao and business logic classes.
I was checking, if we use EJB in the business layer (there will be only a single EJB in the business layer), is there any option to load the spring context in the App Server level or, any where else. Even, can it be loaded in the ejb-jar.xml ? (may be using env-entry) I am not sure which one to chose, but atleast I am sure that, if the spring context can be loaded, it can make lots of implementations easier.