初始化某个repository

(1)问题描述:启动时,要对多个模块进行数据初始化,并将数据放入缓存,如:
attributeRepository.init();
analyticRepository.init();
categoryRepository.init();
subCategoryRepository.init();
scheduleRepository.init();
... ...
nodeRepository.init();
现在的问题是:系统操作过程中,需要对某个模块单独进行初始化,而某个模块初始化前,可能需要先初始化其依赖的模块。
(2)思路:每个模块对应一个字符串,该字符串与service或repository同名,如“attributeRepository”、“analyticRepository”等。然后将该字符串及service或repository放入map中,

这样就可以根据传入的字符串得到要初始化的模块。接着,需要定义一个map,其key存放模块对应的字符串,value为该字符串对应的repository所依赖的其他repositorys。
(3)具体做法:
@Autowired
private NodeRepository nodeRepository;
@Autowired
private RuleRepository ruleRepository;
@Autowired
private ScheduleRepository scheduleRepository;
@Autowired
private AttributeRepository attributeRepository;
@Autowired
private AnalyticRepository analyticRepository;
@Autowired
private CurrencyRepository currencyRepository;
@Autowired
private CategoryRepository categoryRepository;
@Autowired
private SubCategoryRepository subCategoryRepository;
/**

  • 传入参数,执行下面的这个方法 / private void initRepositorys(String repository) throws SecurityException, NoSuchFieldException, InstantiationException, IllegalAccessException{ InitializingRepository initializingRepository = this.getRepository(repository); //获得依赖的初始化repositorys InitializingRepository[] replyOnRepositorysMap = this.getReplyOnRepositorys(repository); //对某个repository执行初始化 this.refreshRepository(initializingRepository, replyOnRepositorysMap); }
    /
    *
    • 获取要初始化的repository
    • @param repository
    • @return / public InitializingRepository getRepository(String repository){ Map repositorysMap = new HashMap(); repositorysMap.put("attributeRepository", attributeRepository); repositorysMap.put("analyticRepository", analyticRepository); repositorysMap.put("categoryRepository", categoryRepository); repositorysMap.put("subCategoryRepository", subCategoryRepository); repositorysMap.put("scheduleRepository", scheduleRepository); repositorysMap.put("nodeRepository", nodeRepository); repositorysMap.put("currencyRepository", currencyRepository); repositorysMap.put("ruleRepository", ruleRepository); repositorysMap.put("holdingHistoryModelRepository", holdingHistoryModelRepository); repositorysMap.put("ruleTemplateRepository", ruleTemplateRepository); repositorysMap.put("ruleMnemonicRepository", ruleMnemonicRepository); repositorysMap.put("incidentRepository", incidentRepository); return repositorysMap.get(repository); }
      /
      *
    • 获取要初始化的repository相关的repositorys
    • @param repository
    • @return / public InitializingRepository[] getReplyOnRepositorys(String repository){ Map replyOnRepositorysMap = new HashMap(); replyOnRepositorysMap.put("incidentRepository", new InitializingRepository[]{attributeRepository}); return replyOnRepositorysMap.get(repository); }
      /
      *
    • 对某个repository执行初始化
    • @param initializingRepository
    • @param replyOnRepositorysMap / public void refreshRepository(InitializingRepository initializingRepository, InitializingRepository[] replyOnRepositorysMap){ try { //先初始化依赖的repositorys,再初始化repository if(replyOnRepositorysMap!=null && replyOnRepositorysMap.length>0){ for(int i=0; i<replyOnRepositorysMap.length; i++){ replyOnRepositorysMap[i].init(); } } initializingRepository.init(); } catch (RepositoryInitializingException e) { e.printStackTrace(); } } private void initRepositorys(String repository) throws SecurityException, NoSuchFieldException, InstantiationException, IllegalAccessException{ InitializingRepository initializingRepository = this.getRepository(repository); //获得依赖的初始化repositorys InitializingRepository[] replyOnRepositorysMap = this.getReplyOnRepositorys(repository); //对某个repository执行初始化 this.refreshRepository(initializingRepository, replyOnRepositorysMap); } /*
    • 获取要初始化的repository
    • @param repository
    • @return / public InitializingRepository getRepository(String repository){ Map repositorysMap = new HashMap(); repositorysMap.put("attributeRepository", attributeRepository); repositorysMap.put("analyticRepository", analyticRepository); repositorysMap.put("categoryRepository", categoryRepository); repositorysMap.put("subCategoryRepository", subCategoryRepository); repositorysMap.put("scheduleRepository", scheduleRepository); repositorysMap.put("nodeRepository", nodeRepository); repositorysMap.put("currencyRepository", currencyRepository); repositorysMap.put("ruleRepository", ruleRepository); repositorysMap.put("holdingHistoryModelRepository", holdingHistoryModelRepository); repositorysMap.put("ruleTemplateRepository", ruleTemplateRepository); repositorysMap.put("ruleMnemonicRepository", ruleMnemonicRepository); repositorysMap.put("incidentRepository", incidentRepository); return repositorysMap.get(repository); } /*
    • 获取要初始化的repository相关的repositorys
    • @param repository
    • @return / public InitializingRepository[] getReplyOnRepositorys(String repository){ Map replyOnRepositorysMap = new HashMap(); replyOnRepositorysMap.put("incidentRepository", new InitializingRepository[]{attributeRepository}); return replyOnRepositorysMap.get(repository); } /*
    • 对某个repository执行初始化
    • @param initializingRepository
    • @param replyOnRepositorysMap / public void refreshRepository(InitializingRepository initializingRepository, InitializingRepository[] replyOnRepositorysMap){ try { //先初始化依赖的repositorys,再初始化repository if(replyOnRepositorysMap!=null && replyOnRepositorysMap.length>0){ for(int i=0; i, InitializingRepository { /*
    • 根据nodeIds查询非close且状态为A的incidents
    • @param nodeIds
    • @return
    • @throws RepositoryException / public List getIncidentsByNodeIds(Collection nodeIds) throws RepositoryException; /*
    • 根据IncidentId查询Incident
    • @param id
    • @return / public Incident getIncidentById(Integer incidentId); /*
    • 保存incident
    • @param incident */ public void saveOrUpdateIncident(Incident incident) throws RepositoryException;
      } 在InitializingRepository 中定义了init方法(具体init方法执行了哪些操作略)。 public interface InitializingRepository { public void init() throws RepositoryInitializingException;
      }
查看全部
zkddkz2010
zkddkz2010
2015/06/25 09:03
  • repository
  • 点赞
  • 收藏
  • 回答
    私信

0个回复