项目中使用了其他项目中的服务,当服务不可用或者报错的时候,项目启动不了,需要解除强依赖

例如之前的写法:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
@Configuration
public class EpasBeanConfig {

@Bean
public GroupOrganizationService getGroupOrganizationService(){
    return EpasClientFactory.createClient(GroupOrganizationService.class,Constants.EPAS_USER_BASIS_APP_KEY);
}

@Bean
public DepartmentService getDepartmentService(){
    return EpasClientFactory.createClient(DepartmentService.class, Constants.EPAS_USER_BASIS_APP_KEY);
}
}
@Autowired
private GroupOrganizationService groupOrganizationService;

@Autowired
private DepartmentService departmentService;

当服务不可用的时候,项目启动失败

修改后的方法:

@Configuration
public class EpasBeanConfig {

    @Bean
    @Lazy
    public GroupOrganizationService getGroupOrganizationService(){
        return EpasClientFactory.createClient(GroupOrganizationService.class, Constants.EPAS_USER_BASIS_APP_KEY);
    }
    
    @Bean
    @Lazy
    public DepartmentService getDepartmentService(){
        return EpasClientFactory.createClient(DepartmentService.class, Constants.EPAS_USER_BASIS_APP_KEY);
    }
}
GroupOrganizationService groupOrganizationService = AppContextHolder.getContext().getBean(GroupOrganizationService.class);

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄