Spring项目按需加载业务功能

在Spring业务开发过程中,有的项目可能需要按需加载某些业务功能。
此时笔者一般使用两种方式:

  1. 注解控制(推荐此方式)
  2. 配置项控制

注解控制(推荐使用)
业务代码类

/**
 * created at 2021/11/2 10:21 上午
 * 页面数据源业务配置上报
 *
 * @author somnuszpli
 */
@Slf4j
public class ThemeRoConfigUploadService implements ApplicationListener<ApplicationReadyEvent> {

    private static final String DATA_SOURCE_TOPIC = "upload.themeRoConfig";

    private static ObjectMapper objectMapper = new ObjectMapper();

    @Value("classpath:themeRo/themeRoConfig.json")
    private Resource dataSourceConfigFile;

    @Autowired
    private EventPublishService eventPublishService;

    @Autowired
    private SystemUserRequestContextInitHelper systemUserRequestContextInitHelper;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        LOG.info("ThemeRoConfig started!");
        try {
        	// 初始化应用信息
            systemUserRequestContextInitHelper.initApplicationContext();
            uploadConfigMessage();
        } finally {
        	// 清除应用信息
            systemUserRequestContextInitHelper.cleanApplicationContext();
        }
    }

	/**
	 * 业务操作
	 */
    private void uploadConfigMessage() {
        LOG.info("Service send ThemeRoConfig message!");
        try {
            JsonNode jsonNode = objectMapper.readTree(dataSourceConfigFile.getInputStream());
            eventPublishService.createDoBoMessage(DATA_SOURCE_TOPIC, jsonNode);
        } catch (Exception e) {
            LOG.error("Read ThemeRoConfig Error: {}", e.getMessage(), e);
        }
    }
}

条件注解。只有添加了@FrmEnableThemeRoUpload注解的项目才可以使用ThemeRoConfigUploadService业务功能。

/**
 * created at 2021/11/8 11:30 上午
 *
 * @author somnuszpli
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(FrmEnableThemeRoUpload.FrmEnableThemeRoConfiguration.class)
public @interface FrmEnableThemeRoUpload {

    @ConditionalOnProperty(value = "kylin.themeRoUploadEnabled", havingValue = "true", matchIfMissing = true)
    @Slf4j
    class FrmEnableThemeRoConfiguration {

        @Bean
        public ThemeRoConfigUploadService frmEnableThemeRoUpload(){
            return new ThemeRoConfigUploadService();
        }

        @PostConstruct
        private void init() {
            LOG.info("@FrmEnableThemeRoUpload support started.");
        }
    }

}

在需要使用的项目的启动类上添加@FrmEnableThemeRoUpload注解。

@FrmEnableThemeRoUpload
public class HomepageApplication {
}

配置项控制

/**
 * created at 2021/11/2 10:21 上午
 * 页面数据源业务配置上报
 *
 * @author somnuszpli
 */
@Slf4j
public class ThemeRoConfigUploadService implements ApplicationListener<ApplicationReadyEvent> {

    private static final String DATA_SOURCE_TOPIC = "upload.themeRoConfig";

    private static ObjectMapper objectMapper = new ObjectMapper();

    @Value("${kylin.uploadThemeRoConfig:#{false}}")
    private boolean uploadThemeRoConfig;

    @Value("classpath:themeRo/themeRoConfig.json")
    private Resource dataSourceConfigFile;

    @Autowired
    private EventPublishService eventPublishService;

    @Autowired
    private SystemUserRequestContextInitHelper systemUserRequestContextInitHelper;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        LOG.info("ThemeRoConfig started!");
        if (!uploadThemeRoConfig) {
            LOG.info("Upload ThemeRo config switch is FALSE!");
            return;
        }
        try {
            systemUserRequestContextInitHelper.initApplicationContext();
            uploadConfigMessage();
        } finally {
            systemUserRequestContextInitHelper.cleanApplicationContext();
        }
    }

    private void uploadConfigMessage() {
        LOG.info("Service send ThemeRoConfig message!");
        try {
            JsonNode jsonNode = objectMapper.readTree(dataSourceConfigFile.getInputStream());
            eventPublishService.createDoBoMessage(DATA_SOURCE_TOPIC, jsonNode);
        } catch (Exception e) {
            LOG.error("Read ThemeRoConfig Error: {}", e.getMessage(), e);
        }
    }
}

在需要使用此功能的服务中添加配置项

kylin:
  uploadThemeRoConfig: true
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值