|
@@ -28,18 +28,39 @@ import org.hsweb.ezorm.rdb.render.dialect.H2RDBDatabaseMetaData;
|
|
|
import org.hsweb.ezorm.rdb.render.dialect.MysqlRDBDatabaseMetaData;
|
|
|
import org.hsweb.ezorm.rdb.render.dialect.OracleRDBDatabaseMetaData;
|
|
|
import org.hsweb.ezorm.rdb.simple.SimpleDatabase;
|
|
|
+import org.hswebframework.expands.script.engine.DynamicScriptEngine;
|
|
|
+import org.hswebframework.expands.script.engine.DynamicScriptEngineFactory;
|
|
|
+import org.hswebframework.web.AopUtils;
|
|
|
+import org.hswebframework.web.ScriptScope;
|
|
|
+import org.hswebframework.web.dao.Dao;
|
|
|
import org.hswebframework.web.datasource.DataSourceHolder;
|
|
|
import org.hswebframework.web.datasource.DatabaseType;
|
|
|
+import org.hswebframework.web.service.Service;
|
|
|
import org.hswebframework.web.starter.init.SystemInitialize;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
import org.springframework.boot.CommandLineRunner;
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.core.Ordered;
|
|
|
+import org.springframework.core.annotation.AnnotationUtils;
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.util.ClassUtils;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import javax.script.ScriptEngine;
|
|
|
import javax.sql.DataSource;
|
|
|
import java.sql.Connection;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
|
|
|
* @author zhouhao
|
|
@@ -47,7 +68,7 @@ import java.sql.Connection;
|
|
|
@Configuration
|
|
|
@EnableConfigurationProperties(AppProperties.class)
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
-public class SystemInitializeAutoConfiguration implements CommandLineRunner {
|
|
|
+public class SystemInitializeAutoConfiguration implements CommandLineRunner, BeanPostProcessor {
|
|
|
|
|
|
@Autowired
|
|
|
private AppProperties appProperties;
|
|
@@ -58,6 +79,27 @@ public class SystemInitializeAutoConfiguration implements CommandLineRunner {
|
|
|
@Autowired
|
|
|
SqlExecutor sqlExecutor;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ApplicationContext applicationContext;
|
|
|
+
|
|
|
+ private List<DynamicScriptEngine> engines;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ engines = Stream.of("js", "groovy")
|
|
|
+ .map(DynamicScriptEngineFactory::getEngine)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ addGlobalVariable("logger", LoggerFactory.getLogger("org.hswebframework.script"));
|
|
|
+ addGlobalVariable("sqlExecutor", sqlExecutor);
|
|
|
+ addGlobalVariable("spring", applicationContext);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void addGlobalVariable(String var, Object val) {
|
|
|
+ engines.forEach(engine -> engine.addGlobalVariable(Collections.singletonMap(var, val)));
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void run(String... args) throws Exception {
|
|
|
DatabaseType type = DataSourceHolder.currentDatabaseType();
|
|
@@ -94,4 +136,20 @@ public class SystemInitializeAutoConfiguration implements CommandLineRunner {
|
|
|
|
|
|
initialize.install();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
|
|
+ if (bean instanceof Service) {
|
|
|
+ addGlobalVariable(beanName, bean);
|
|
|
+ } else if (AnnotationUtils.findAnnotation(ClassUtils.getUserClass(bean), ScriptScope.class) != null) {
|
|
|
+ addGlobalVariable(beanName, bean);
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
}
|