1. 实现 ApplicationRunner run 接口
2. 通过 SpringContextUtil.getBean(xxx.class) 获取指定bean
openfeign 的服务 (远程调用服务)
TenantService tenantService = SpringContextUtil.getBean(TenantService.class);
总的示例代码如下
@Component
public class LicenseValid implements ApplicationRunner {
Logger logger = LoggerFactory.getLogger("LicenseValid");
public LicenseValid() {
//System.out.println(tenantMapper);
}
@Override
public void run(ApplicationArguments args) throws Exception {
TenantService tenantService = SpringContextUtil.getBean(TenantService.class);
Integer total = null;
try{
total = LicenseUtils.getMaxFormCount();
if(total == null)
{
throw new RuntimeException("授权文件未配置租户授权数量相关信息");
}
}catch (Exception e)
{
e.printStackTrace();
logger.error("{}:{}",e.getStackTrace(),e.getMessage());
System.exit(1);
}
try {
Integer nums = null;
try
{
nums = tenantService.getUsedFormNum().getData();
}catch (ClientException e)
{
e.getErrorMessage();
}
if(nums == null)
{
throw new RuntimeException("获取已授权租户表单授权数量失败");
}
if(total < nums)
{
throw new RuntimeException("租户授权数量不正确,超过授权数量限制");
}
}catch (RuntimeException e)
{
e.printStackTrace();
if(e.getMessage().indexOf("Load balancer does not have available server for client: form3-core") != -1)
{
logger.error("请先启动Core服务!!!");
}else{
logger.error("{}:{}",e.getStackTrace(),e.getMessage());
}
System.exit(1);
}
}
}
3. 可以通过依赖注入 直接获取 mybatis Mapper,本地调用 @Autowired
@Component
public class LicenseValid implements ApplicationRunner {
Logger logger = LoggerFactory.getLogger("LicenseValid");
@Autowired
TenantMapper tenantMapper;
public LicenseValid() {
//System.out.println(tenantMapper);
}
//已用总数统计
public Integer usedNumCount()
{
QueryWrapper<Tenant> queryWrapper = new QueryWrapper<>();
List<Tenant> tenants = tenantMapper.selectList(queryWrapper);
//List<Integer> nums = tenants.stream().map(tenant -> tenant.getFormNum()).collect(Collectors.toList());
Integer nums = 0;
if( tenants != null && tenants.size() != 0)
{
for( Tenant tenant : tenants)
{
nums = nums + tenant.getFormNum();
}
}
return nums;
}
@Override
public void run(ApplicationArguments args) throws Exception {
Integer total = null;
try{
total = LicenseUtils.getMaxFormCount();
if(total == null)
{
throw new RuntimeException("授权文件未配置租户授权数量相关信息");
}
}catch (Exception e)
{
e.printStackTrace();
logger.error("{}:{}",e.getStackTrace(),e.getMessage());
System.exit(1);
}
try {
int nums = usedNumCount();
//System.out.println(nums);
if(total < nums)
{
//System.out.println(nums);
throw new RuntimeException("租户授权数量不正确,超过授权数量限制");
}
}catch (RuntimeException e)
{
e.printStackTrace();
logger.error("{}:{}",e.getStackTrace(),e.getMessage());
System.exit(1);
}
}
}