Spring Multi-Tenant Architecture-Practical Implementation: August 2016
Spring Multi-Tenant Architecture-Practical Implementation: August 2016
net/publication/321070490
CITATION READS
1 375
1 author:
Ahmed Ali
University of the Cumberlands
18 PUBLICATIONS 10 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Ahmed Ali on 15 November 2017.
Abstract
Multi-tenancy architecture is often used where single instance of software or components run on server
the central idea is that multiple tenant applications share single instance or components stored in the
infrastructure Meta data., In other words, in Multi-tenancy architecture each tenant application not only
needs to develop its own functionalities, but also needs to prepare an infrastructure to allow its tenants
to run customized applications. This article propose implementation models for Multi-tenancy in Spring
as an example, and discusses their trade-offs including their formal notations and application scenarios.
What is Multi-tenancy?
– A tenant is a group of users sharing the same view of the software instance Under
specific privileges
Benefits of Multi-tenancy
Cost savings
What if we try to implements all types of requirements for all application instances?
Assume we have different instance running on different Application Server and all
have the same application what are the issue of this design?
– Number of components increased if the application tries to handle every specific user or
group of users requirements
– Components will explode more if another group of users requirements joins the to
accommodate all permutations and combinations
– Hard to manage the components and leads to lot of refactoring, defects, and post-
production maintenance problems due to complexity
– Components management difficulties at source control level especially for large scale
– Unnecessary other instance of application specific bean creation issues to support multi-
tenancy that leads to memory bloating
– Need to support customizations for existing program functionality for each version at
various levels such as
– Controllers
– Delegators
– Service classes
– Configuration files
Solution:
Create instance specific beans using spring framework scope functionality by creating
custom scope
Multi-tenancy Scenarios
Code level support for multi-tenancy is needed for three types of requirements:
– Scenario 1: Overriding the existing functionality which is unique to each instance
• Screen level:
– adding new fields
– removing fields,
– customizing the labels
– Custom error or validation messages
• Business logic: each instance may need specific business rules implementation
– Overriding the existing functionality
– Scenario 2: new requirement for one instance only (unique to the specific instance )
– Scenario 3: New requirement for all instance that is same for all
– Scenario 4: New requirement that is common for all instance except different for other
instances
Possible requirements:
Screen fields may be different from baseline and different for each instance
Business logic may be different from baseline and different for each instance
Since it is common data model, data objects such as entity objects, value objects and
command objects are common for all instances. Changes for one instance are applicable
to all with no business impact
Multi-tenancy Support:
• Extend the base class and either override an existing method or add new
method
• Extend the base class and either override an existing method or add new
method
• Database changes
Scenario 2: New requirement for one instance only (unique to each instance)
• Possible requirements:
- New Screen
- New service classes to accommodate business logic
- Database changes to implement instance specific functionality
• No impact to Baseline Code:
- Since it is common data model, data objects such as entity objects,
value objects and command objects are common for all instances.
Changes for one instance are applicable to all with no business impact
• Multi-tenancy Support:
If new screen is needed:
- Custom screen for each instance
- Changes needed at tiles definition configuration file
- Bean id definition changes in Spring configuration file
- No changes needed at controller level
- Delegator classes need to be created and configured
Scenario 3: New requirement for all each instance that is same for all
• Possible requirements:
- New Screen
- New service classes to accommodate business logic
- Database changes to implement instance specific functionality
• Multi-tenancy Support:
- All the steps are same as Scenario 2 but all the files are put at base
product level instead of instance specific custom package level
Scenario 4: New requirement that is common for more than one instance and
different for other
Possible requirements:
- New Screen
- New service classes to accommodate business logic
- Database changes to implement instance specific functionality
Multi-tenancy Support:
- The requirements that are common for more than one instance follows
scenario 3 flow and all the changes and new files are part of product
level
- The requirements unique for one follow scenario 1 flow and the new
files are placed in respective instance packages
All custom JSP and resource files must be kept under that each instance specific folder in
webapp
If overriding the existing screen, name the new jsp file name same as base jsp filename. This is
really important to retain the navigational flow.
All new instance specific class files must kept in the instance specific package. The format for the
package name is
– <instance code>.<domain>.<hluc name>.class name
Example: ga.benefits.applyforbenefits.ClaimEntry.java
– The classes should be kept in the respective base component. For example, base
ClaimEntry.java file is in uilink.benefits.applyforbenefits package in Benefits component.
So keep the new file in the same component under new package name mentioned
above
Each instance will have its own version of Tiles definitions, message.properties,
errormessage.properties, spring configuration files such as uiconnect-servlet.xml.
– Tiles definition files for the product is renamed as definitions-product.xml
– Tiles definition files for each instance are created as definitions-GA.xml, definitions-
SC.xml and definitions-NC.xml
– Each instance has its own message properties file with the name message-<instance
abbreviation>.xml
@Override
throws BeansException {
appCtx = appContext;
return appCtx;
return tenantName;
this.tenantName = tenantName;
@SuppressWarnings("rawtypes")
if (tenantName == null) {
try {
this.tenantName = initTenantName();
} catch (NamingException e) {
e.printStackTrace();
if (multitenantScopeClassMap == null) {
initMultitenantScopeClassMap();
if (objectMap.containsKey(beanName)) {
return objectMap.get(beanName);
if (PRODUCT_TENANT.equalsIgnoreCase(tenantName)) {
objectMap.put(beanName, objectFactory.getObject());
return objectMap.get(beanName);
if (multitenantScopeClassMap.containsKey(beanName)) {
try {
AutowireCapableBeanFactory beanFactory =
appCtx.getAutowireCapableBeanFactory();
objectMap.put(beanName,
beanFactory.createBean(myclass.newInstance().getClass()));
return objectMap.get(beanName);
objectMap.put(beanName, objectFactory.getObject());
return objectMap.get(beanName);
tenantName=multitenantScopeDAO.getSystemParametersInstanceCode();
return tenantName;
try {
List<AcuityBeanClassesEO> acuityBeanClassesEOList =
multitenantScopeDAO.getMultitenantClasses(tenantName.toUpperCase());
while(iterator.hasNext()){
tenantScopeClassMap.put(acuityBeanClassesEO.getPrimaryKey().getBeanId(),acuityBea
nClassesEO.getClassNm());
}catch (Exception e) {
throw new RuntimeException(e);
return objectMap.remove(name);
return "MultiTenantScope";
bean config
– You can use either command line or STS Run Configuration utility to compile the source
code targeted for a specific instance
– Pass the profile name of the instance you are targeting compile to the maven install
command as shown below. For example:
Build process for a Specific Instance in IDE using Run Configuration Utility
The source code can be compiled using run configuration utility by passing the maven profile
name as an argument.
3. In the Create, manage and run configurations screen, click the New launch configuration
icon.
7. Click the Run button to start the build process for the project
Summary
Multitenant support at
– Entity object, Value Object and Command will be the same for all Instances. If a
property is required for the Instance, it will be added and will remain null all the time
for other Instances.
2. Presentation layer
– Business layer
– Create a class for each instance extending the product class. The instance specific class
will override the functionality in the parent class.
– The definition for each bean which needs to be customized (delegator, service, dao,
validator, etc) will be changed to set the bean scope as multitenant. This is required
just for beans which will be customized.