在Spring业务开发过程中,有的项目可能需要按需加载某些业务功能。
此时笔者一般使用两种方式:
- 注解控制(推荐此方式)
- 配置项控制
注解控制(推荐使用)
业务代码类
/**
* 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