I'm working on a Spring Boot application and I'm trying to inject my UserService class into my TenantIdentifierResolver class because I want to use the createUser() method. However i get a nullpointer exception. For some reason userService is set to null, what am I missing here?
@Component
public class TenantIdentifierResolver implements CurrentTenantIdentifierResolver {
public static final String DEFAULT_TENANT = "default_schema";
private UserService userService;
@Override
public String resolveCurrentTenantIdentifier() {
String tenant = TenantContext.getCurrentTenant();
if(tenant != null){
userService.createUser(tenant);
return tenant;
} else {
return DEFAULT_TENANT;
}
}
@Override
public boolean validateExistingCurrentSessions() {
return true;
}
}
I've tried to use @Autowired or make a constructor injection but then I got this error:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| entityManagerFactory defined in class path resource [org/example/membership/config/HibernateConfig.class]
↑ ↓
| tenantIdentifierResolver (field private org.example.membership.service.UserService org.example.membership.multitenancy.TenantIdentifierResolver.userService)
↑ ↓
| userService defined in file [C:\project\Member\server\target\classes\org\example\membership\service\UserService.class]
↑ ↓
| userRepository defined in org.example.membership.repository.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration
↑ ↓
| (inner bean)#18957a3d
└─────┘
This is my UserService class
@Service
public class UserService {
private UserRepository userRepository;
private TenantService tenantService;
public UserService(UserRepository repository, TenantService tenantService) {
this.userRepository = repository;
this.tenantService = tenantService;
}
@Transactional
public User createUser(String tenant) {
Tenant tenant = new Tenant();
tenant.setTenantName(tenant);
tenantService.initDatabase(tenant);
return tenant ;
}
}
This is my HibernateConfig class
@Configuration
public class HibernateConfig {
@Autowired
private JpaProperties jpaProperties;
@Bean
JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource,
MultiTenantConnectionProvider multiTenantConnectionProviderImpl,
CurrentTenantIdentifierResolver currentTenantIdentifierResolverImpl
) {
Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
jpaPropertiesMap.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProviderImpl);
jpaPropertiesMap.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifierResolverImpl);
LocalContainerEntityManagerFactoryBean em = new
LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("org.example*");
em.setJpaVendorAdapter(this.jpaVendorAdapter());
em.setJpaPropertyMap(jpaPropertiesMap);
return em;
}
}