【7.0.4】更新listener适配cloud版本多spring环境

pull/16/MERGE
fengshuonan 2021-05-14 21:58:45 +08:00
parent 536f4b0a8c
commit 9f819d88e8
11 changed files with 224 additions and 55 deletions

View File

@ -0,0 +1,64 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.rule.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* application ready
*
* @author fengshuonan
* @date 2021/5/14 20:28
*/
@Slf4j
public abstract class ApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// 如果是配置中心的上下文略过spring cloud环境environment会读取不到
ConfigurableApplicationContext applicationContext = event.getApplicationContext();
if (applicationContext instanceof AnnotationConfigApplicationContext) {
return;
}
// 执行具体业务
this.eventCallback(event);
}
/**
*
*
* @author fengshuonan
* @date 2021/5/14 20:17
*/
public abstract void eventCallback(ApplicationReadyEvent event);
}

View File

@ -0,0 +1,64 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.rule.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* application
*
* @author fengshuonan
* @date 2021/5/14 20:28
*/
@Slf4j
public abstract class ApplicationStartedListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
// 如果是配置中心的上下文略过spring cloud环境environment会读取不到
ConfigurableApplicationContext applicationContext = event.getApplicationContext();
if (applicationContext instanceof AnnotationConfigApplicationContext) {
return;
}
// 执行具体业务
this.eventCallback(event);
}
/**
*
*
* @author fengshuonan
* @date 2021/5/14 20:17
*/
public abstract void eventCallback(ApplicationStartedEvent event);
}

View File

@ -0,0 +1,64 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.rule.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* context
*
* @author fengshuonan
* @date 2021/5/14 20:28
*/
@Slf4j
public abstract class ContextInitializedListener implements ApplicationListener<ApplicationContextInitializedEvent> {
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent event) {
// 如果是配置中心的上下文略过spring cloud环境environment会读取不到
ConfigurableApplicationContext applicationContext = event.getApplicationContext();
if (applicationContext instanceof AnnotationConfigApplicationContext) {
return;
}
// 执行具体业务
this.eventCallback(event);
}
/**
*
*
* @author fengshuonan
* @date 2021/5/14 20:17
*/
public abstract void eventCallback(ApplicationContextInitializedEvent event);
}

View File

@ -33,11 +33,9 @@ import cn.stylefeng.roses.kernel.config.api.exception.ConfigException;
import cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExceptionEnum;
import cn.stylefeng.roses.kernel.config.modular.factory.SysConfigDataFactory;
import cn.stylefeng.roses.kernel.rule.context.ApplicationPropertiesContext;
import cn.stylefeng.roses.kernel.rule.listener.ContextInitializedListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
@ -58,7 +56,7 @@ import static cn.stylefeng.roses.kernel.config.api.exception.enums.ConfigExcepti
* @date 2020/6/6 23:39
*/
@Slf4j
public class ConfigInitListener implements ApplicationListener<ApplicationContextInitializedEvent>, Ordered {
public class ConfigInitListener extends ContextInitializedListener implements Ordered {
@Override
public int getOrder() {
@ -66,19 +64,13 @@ public class ConfigInitListener implements ApplicationListener<ApplicationContex
}
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent applicationContextInitializedEvent) {
// 如果是配置中心的上下文略过spring cloud环境environment会读取不到
ConfigurableApplicationContext applicationContext = applicationContextInitializedEvent.getApplicationContext();
if (applicationContext instanceof AnnotationConfigApplicationContext) {
return;
}
public void eventCallback(ApplicationContextInitializedEvent event) {
// 初始化Config Api
ConfigContext.setConfigApi(new ConfigContainer());
// 获取environment参数
ConfigurableEnvironment environment = applicationContextInitializedEvent.getApplicationContext().getEnvironment();
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
// 初始化ApplicationPropertiesContext
ApplicationPropertiesContext.getInstance().initConfigs(environment);
@ -116,7 +108,6 @@ public class ConfigInitListener implements ApplicationListener<ApplicationContex
} finally {
DbUtil.close(conn);
}
}
}

View File

@ -28,10 +28,10 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.exception.DaoException;
import cn.stylefeng.roses.kernel.db.api.exception.enums.FlywayExceptionEnum;
import cn.stylefeng.roses.kernel.rule.listener.ContextInitializedListener;
import lombok.extern.slf4j.Slf4j;
import org.flywaydb.core.Flyway;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@ -45,15 +45,14 @@ import org.springframework.jdbc.datasource.DriverManagerDataSource;
* @date 2021/1/17 21:14
*/
@Slf4j
public class FlywayInitListener implements ApplicationListener<ApplicationContextInitializedEvent>, Ordered {
public class FlywayInitListener extends ContextInitializedListener implements Ordered {
private static final String FLYWAY_LOCATIONS = "classpath:db/migration";
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent applicationEnvironmentPreparedEvent) {
public void eventCallback(ApplicationContextInitializedEvent event) {
ConfigurableEnvironment environment = applicationEnvironmentPreparedEvent.getApplicationContext().getEnvironment();
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
// 获取数据库连接配置
String dataSourceUrl = environment.getProperty("spring.datasource.url");
@ -103,7 +102,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
// 是否开启占位符
boolean enablePlaceholder = true;
if(StrUtil.isNotBlank(placeholder)){
if (StrUtil.isNotBlank(placeholder)) {
enablePlaceholder = Boolean.parseBoolean(placeholder);
}
@ -142,6 +141,7 @@ public class FlywayInitListener implements ApplicationListener<ApplicationContex
log.error("flyway初始化失败", e);
throw new DaoException(FlywayExceptionEnum.FLYWAY_MIGRATE_ERROR, e.getMessage());
}
}
@Override

View File

@ -25,9 +25,9 @@
package cn.stylefeng.roses.kernel.db.init.listener;
import cn.stylefeng.roses.kernel.db.init.actuator.DbInitializer;
import cn.stylefeng.roses.kernel.rule.listener.ApplicationReadyListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import java.util.Map;
@ -39,18 +39,15 @@ import java.util.Map;
* @date 2018/4/23 9:57
*/
@Slf4j
public class InitTableListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {
public class InitTableListener extends ApplicationReadyListener implements Ordered {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
public void eventCallback(ApplicationReadyEvent event) {
Map<String, DbInitializer> beansOfType = event.getApplicationContext().getBeansOfType(DbInitializer.class);
for (Map.Entry<String, DbInitializer> entry : beansOfType.entrySet()) {
DbInitializer value = entry.getValue();
value.dbInit();
}
}
@Override

View File

@ -30,12 +30,10 @@ import cn.stylefeng.roses.kernel.db.api.factory.DruidDatasourceFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.druid.DruidProperties;
import cn.stylefeng.roses.kernel.dsctn.api.exception.DatasourceContainerException;
import cn.stylefeng.roses.kernel.dsctn.context.DataSourceContext;
import cn.stylefeng.roses.kernel.rule.listener.ContextInitializedListener;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
@ -50,7 +48,7 @@ import static cn.stylefeng.roses.kernel.dsctn.api.exception.enums.DatasourceCont
* @date 2020/11/1 0:02
*/
@Slf4j
public class DataSourceInitListener implements ApplicationListener<ApplicationContextInitializedEvent>, Ordered {
public class DataSourceInitListener extends ContextInitializedListener implements Ordered {
@Override
public int getOrder() {
@ -58,15 +56,9 @@ public class DataSourceInitListener implements ApplicationListener<ApplicationCo
}
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent applicationContextInitializedEvent) {
public void eventCallback(ApplicationContextInitializedEvent event) {
// 如果是配置中心的上下文略过spring cloud环境environment会读取不到
ConfigurableApplicationContext applicationContext = applicationContextInitializedEvent.getApplicationContext();
if (applicationContext instanceof AnnotationConfigApplicationContext) {
return;
}
ConfigurableEnvironment environment = applicationContextInitializedEvent.getApplicationContext().getEnvironment();
ConfigurableEnvironment environment = event.getApplicationContext().getEnvironment();
// 获取数据库连接配置
String dataSourceDriver = environment.getProperty("spring.datasource.driver-class-name");
@ -100,4 +92,5 @@ public class DataSourceInitListener implements ApplicationListener<ApplicationCo
}
}
}

View File

@ -28,9 +28,9 @@ import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.i18n.api.TranslationApi;
import cn.stylefeng.roses.kernel.i18n.api.TranslationPersistenceApi;
import cn.stylefeng.roses.kernel.i18n.api.pojo.TranslationDict;
import cn.stylefeng.roses.kernel.rule.listener.ApplicationStartedListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import java.util.List;
@ -41,10 +41,10 @@ import java.util.List;
* @date 2021/1/24 19:36
*/
@Slf4j
public class TranslationDictInitListener implements ApplicationListener<ApplicationStartedEvent> {
public class TranslationDictInitListener extends ApplicationStartedListener {
@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
public void eventCallback(ApplicationStartedEvent event) {
TranslationPersistenceApi tanTranslationPersistenceApi = SpringUtil.getBean(TranslationPersistenceApi.class);
TranslationApi translationApi = SpringUtil.getBean(TranslationApi.class);
@ -55,7 +55,6 @@ public class TranslationDictInitListener implements ApplicationListener<Applicat
translationApi.init(allTranslationDict);
log.info("初始化所有的翻译字典" + allTranslationDict.size() + "条!");
}
}
}

View File

@ -24,6 +24,7 @@
*/
package cn.stylefeng.roses.kernel.scanner;
import cn.stylefeng.roses.kernel.rule.listener.ApplicationReadyListener;
import cn.stylefeng.roses.kernel.scanner.api.ResourceCollectorApi;
import cn.stylefeng.roses.kernel.scanner.api.ResourceReportApi;
import cn.stylefeng.roses.kernel.scanner.api.constants.ScannerConstants;
@ -33,9 +34,7 @@ import cn.stylefeng.roses.kernel.scanner.api.pojo.resource.ResourceDefinition;
import cn.stylefeng.roses.kernel.scanner.api.pojo.scanner.ScannerProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.Ordered;
import java.util.Map;
@ -47,16 +46,12 @@ import java.util.Map;
* @date 2020/10/19 22:27
*/
@Slf4j
public class ResourceReportListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {
public class ResourceReportListener extends ApplicationReadyListener implements Ordered {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
public void eventCallback(ApplicationReadyEvent event) {
// 如果是配置中心的上下文略过spring cloud环境environment会读取不到
ConfigurableApplicationContext applicationContext = event.getApplicationContext();
if (applicationContext instanceof AnnotationConfigApplicationContext) {
return;
}
// 获取有没有开资源扫描开关
ScannerProperties scannerProperties = applicationContext.getBean(ScannerProperties.class);

View File

@ -25,6 +25,7 @@
package cn.stylefeng.roses.kernel.timer.modular.listener;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.roses.kernel.rule.listener.ApplicationStartedListener;
import cn.stylefeng.roses.kernel.timer.api.TimerExeService;
import cn.stylefeng.roses.kernel.timer.api.enums.TimerJobStatusEnum;
import cn.stylefeng.roses.kernel.timer.modular.entity.SysTimers;
@ -32,7 +33,6 @@ import cn.stylefeng.roses.kernel.timer.modular.param.SysTimersParam;
import cn.stylefeng.roses.kernel.timer.modular.service.SysTimersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import java.util.List;
@ -44,10 +44,10 @@ import java.util.List;
* @date 2021/1/12 20:40
*/
@Slf4j
public class TaskRunListener implements ApplicationListener<ApplicationStartedEvent>, Ordered {
public class TaskRunListener extends ApplicationStartedListener implements Ordered {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
public void eventCallback(ApplicationStartedEvent event) {
SysTimersService sysTimersService = SpringUtil.getBean(SysTimersService.class);
TimerExeService timerExeService = SpringUtil.getBean(TimerExeService.class);
@ -69,10 +69,12 @@ public class TaskRunListener implements ApplicationListener<ApplicationStartedEv
log.error("定时器初始化遇到错误,略过该定时器!", exception);
}
}
}
@Override
public int getOrder() {
return LOWEST_PRECEDENCE - 300;
}
}

View File

@ -24,11 +24,11 @@
*/
package cn.stylefeng.roses.kernel.system.starter.listener;
import cn.stylefeng.roses.kernel.rule.listener.ApplicationReadyListener;
import cn.stylefeng.roses.kernel.system.api.constants.SystemConstants;
import cn.stylefeng.roses.kernel.system.starter.init.InitAdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
@ -42,13 +42,13 @@ import javax.annotation.Resource;
*/
@Component
@Slf4j
public class SuperAdminInitListener implements ApplicationListener<ApplicationReadyEvent>, Ordered {
public class SuperAdminInitListener extends ApplicationReadyListener implements Ordered {
@Resource
private InitAdminService initAdminService;
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
public void eventCallback(ApplicationReadyEvent event) {
initAdminService.initSuperAdmin();
}