Rohan
Rohan

Reputation: 266

How to share bean INSTANCE across war in SPRING?

I want to share a singleton bean across multiple war. I know sharing ApplicaitonContext using parentContextKey attribute(Example, http://blog.springsource.org/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/)

But this way instance of bean created multiple (for 2 war, 2 instance). I want only 1 instance across 2 war.

Another way, If i set some value in any POJO, it should be accessible in another war.

Reason i need this is, there are some beans(like HibernateSessionFactory, Datasource etc which are expensive) which are created multiple times(n instance for n war). Whereas i want to utilize same instance instead of creating same in different war.

Can anyone provide me solution for this?

Upvotes: 1

Views: 2563

Answers (2)

Abhishek Kumar
Abhishek Kumar

Reputation: 219

Spring provide a way to expose any bean (service) and these bean can be access from any other web application or any standalone application.

please refer Remoting and Web Service using Spring to get more details.

Upvotes: 1

beny23
beny23

Reputation: 35008

You could achieve this by binding the objects into the global JNDI tree. That means that both WARs would have references to an object looked up in JNDI.

Hibernate allows you to use the hibernate.session_factory_name property (this may well be a good starting point. Data sources should already be looked up from JNDI.

One thing, I would not class a session factory or a data source as expensive, so you may well be saving a miniscule amount of memory in exchange for a lot of additional complexity, so I would ask myself the question on whether this is worth the additional maintenance headaches.

Upvotes: 1

Related Questions