EntityRegistryService: PostConstrust replaced with event listener ContextRefreshedEvent. This will guarantee that all EntityDaoService's beans created. Otherwise, the Bean may discover beans that not created yet (reproducible on dao tests)
This commit is contained in:
parent
e058786956
commit
add0a342c4
@ -15,27 +15,31 @@
|
||||
*/
|
||||
package org.thingsboard.server.dao.entity;
|
||||
|
||||
import com.google.common.collect.MapMaker;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thingsboard.server.common.data.EntityType;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class DefaultEntityServiceRegistry implements EntityServiceRegistry {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
private final Map<EntityType, EntityDaoService> entityDaoServicesMap;
|
||||
private final Map<EntityType, EntityDaoService> entityDaoServicesMap = new MapMaker().weakValues().makeMap();
|
||||
|
||||
public DefaultEntityServiceRegistry(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
this.entityDaoServicesMap = new HashMap<>();
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
@EventListener(ContextRefreshedEvent.class)
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public void init() {
|
||||
log.debug("Initializing EntityServiceRegistry on ContextRefreshedEvent");
|
||||
applicationContext.getBeansOfType(EntityDaoService.class).values().forEach(entityDaoService -> {
|
||||
EntityType entityType = entityDaoService.getEntityType();
|
||||
entityDaoServicesMap.put(entityType, entityDaoService);
|
||||
@ -43,6 +47,7 @@ public class DefaultEntityServiceRegistry implements EntityServiceRegistry {
|
||||
entityDaoServicesMap.put(EntityType.RULE_NODE, entityDaoService);
|
||||
}
|
||||
});
|
||||
log.debug("Initialized EntityServiceRegistry total [{}] entries", entityDaoServicesMap.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user