tag:blogger.com,1999:blog-8023739875307409079.post7347414563595752424..comments2022-09-27T05:04:34.450-07:00Comments on Technical stuff: Setting up a multi tenant environment using the Spring FrameworkOded Peerhttp://www.blogger.com/profile/13092888750242228106noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-8023739875307409079.post-92129259340265786702012-10-24T04:27:01.547-07:002012-10-24T04:27:01.547-07:00I did not understand your problem descrpition, did...I did not understand your problem descrpition, did you create the context once during initialization?<br /><br />I created a simple example using the files above, does this helps you?<br /><br />package test;<br /><br />import java.io.IOException;<br />import java.util.HashMap;<br />import java.util.Map;<br /><br />import javax.servlet.ServletException;<br />import javax.servlet.annotation.WebInitParam;<br />import javax.servlet.annotation.WebServlet;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br /><br />import org.springframework.context.support.ClassPathXmlApplicationContext;<br /><br />@SuppressWarnings("serial")<br />@WebServlet(value = "/*", loadOnStartup = 1, initParams = { @WebInitParam(name = "tenants", value = "a,b") })<br />public class MyServlet extends HttpServlet {<br /><br /> private static Map<String, ClassPathXmlApplicationContext> tenantContexts;<br /><br /> @Override<br /> public void init() throws ServletException {<br /> super.init();<br /> tenantContexts = new HashMap<String, ClassPathXmlApplicationContext>();<br /> String[] tenants = getInitParameter( "tenants" ).split( " *, *" );<br /> // set up all the tenants<br /> for ( String tenant : tenants ) {<br /> // for each tenant create a Spring ApplicationContext<br /> ClassPathXmlApplicationContext tenantCtx = new ClassPathXmlApplicationContext();<br /> tenantCtx.setConfigLocation( "/beans.xml" );<br /> TenantPropertyPlaceholderConfigurer beanFactoryPostProcessor = new TenantPropertyPlaceholderConfigurer( tenant );<br /> tenantCtx.addBeanFactoryPostProcessor( beanFactoryPostProcessor );<br /> tenantCtx.refresh();<br /> tenantContexts.put( tenant, tenantCtx );<br /> }<br /> }<br /><br /> @Override<br /> public void destroy() {<br /> super.destroy();<br /> for ( ClassPathXmlApplicationContext tenantContext : tenantContexts.values() ) {<br /> tenantContext.destroy();<br /> }<br /> tenantContexts.clear();<br /> }<br /><br /> @Override<br /> protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {<br /> String tenant = req.getParameter( "tenant" );<br /> A a = (A)tenantContexts.get( tenant ).getBean( "a" );<br /> resp.getWriter().print( "<html><body><h1>i: " + a.getI() + "</h1>" + "<br><h2>tenant: " + tenant + "</body></html>" );<br /> }<br /><br />}<br /></pre>Oded Peerhttps://www.blogger.com/profile/13092888750242228106noreply@blogger.comtag:blogger.com,1999:blog-8023739875307409079.post-39407837781615035512012-10-04T02:06:14.974-07:002012-10-04T02:06:14.974-07:00Hi Oded Peer,
I followed your post and I am able ...Hi Oded Peer,<br /><br />I followed your post and I am able to get the spring context based on the tenant information. But the issue is, for each request from the tenant, how to use the existing context for that tenant. In my scenario, for each request context is get refreshing.<br /><br />Please provide some help on this....<br /><br />Thank You.narihttps://www.blogger.com/profile/03002790765417285941noreply@blogger.comtag:blogger.com,1999:blog-8023739875307409079.post-27333950552190464372012-04-07T06:13:03.910-07:002012-04-07T06:13:03.910-07:00Leo Technosoft developed its own SaaS framework ca...Leo Technosoft developed its own SaaS framework called "SaaS Tenant™"<br /><br />This is also good for developing cloud base application...<br /><br />http://www.saas-tenant.com/Adminhttps://www.blogger.com/profile/05579864238900195901noreply@blogger.comtag:blogger.com,1999:blog-8023739875307409079.post-79838372412478814022012-03-06T08:01:15.673-08:002012-03-06T08:01:15.673-08:00The simple thing to do is to keep the application ...The simple thing to do is to keep the application contexts in a map, with the tenant as the key.<br />When your application serves a request it must already know which tenant issued the request, thus looking up the tenant's application context is easy.<br /><br />Let's compare this to what Spring provides for web applications. You define your Spring XML configuration, and set up and access beans in your web application by using ContextLoaderListener and WebApplicationContextUtils.<br />In a multi-tenant appplication I would implement javax.servlet.ServletContextListener by myslef (not relying on Spring's ContextLoaderListener), and provide a static getter on that class, which gets the tenant as a parameter and returns the tenant's application context (very similar to what you do with Spring's WebApplicationContextUtils).<br />In both ways, once you have a reference to the application context you can get any bean you defined in your configuration.<br /><br />For more information on what Spring provides to web applications see <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/web-integration.html" rel="nofollow">Spring documentation</a> chapter 17.2Oded Peerhttps://www.blogger.com/profile/13092888750242228106noreply@blogger.comtag:blogger.com,1999:blog-8023739875307409079.post-86175245280610015372012-03-06T06:51:29.204-08:002012-03-06T06:51:29.204-08:00And how do I use your a-bean? Let's say that I...And how do I use your a-bean? Let's say that I (auto)wired it some where, how does the tenant instance get resolved?Anonymousnoreply@blogger.com