Separate instance for particular bean or set of beans for each tenant

Hi,
I am developing a multi-tenant system using multi-tenancy addon, i want to provide a separate instance of a particular bean or set of beans for each tenant. How to create, register and use a custom scope(tenant based scope) in jmix 2 application?

Thanks.
Vimal

Hello,

Jmix is built on top of Spring Boot and does not restrict scope creation and usage. So, an implementation by some standard tutorial (e.g. this one) should have no problems.

Current tenant can be obtained using TenantProvider#getCurrentUserTenantId() (see docs).

Regards,
Dmitry

Hi @taimanov ,
Thank you for the response. I have tried the tutorial. And below are my findings.

  1. The TenantBean Class which is not annotated with @Component. So, we cannot able to inject any jmix object into the TenantBean.
public class TenantBean {
    
    private final String name;
    
    public TenantBean(String name) {
        this.name = name;
    }

    public void sayHello() {
        System.out.println(
          String.format("Hello from %s of type %s",
          this.name, 
          this.getClass().getName()));
    }
}

  1. We have to configure each tenant in the TenantBeansConfig class. So, we unable to create Tenants on runtime.
@Configuration
public class TenantBeansConfig {

    @Scope(scopeName = "tenant")
    @Bean
    public TenantBean foo() {
        return new TenantBean("foo");
    }
    
    @Scope(scopeName = "tenant")
    @Bean
    public TenantBean bar() {
        return new TenantBean("bar");
    }
}

Attached the sample project here multitenancy-test.zip (186.9 KB)
.