代码优化
This commit is contained in:
@@ -29,7 +29,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@SpringBootApplication(exclude = {
|
||||
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
|
||||
})
|
||||
@ServletComponentScan
|
||||
@ServletComponentScan //https://blog.csdn.net/qq_36850813/article/details/101194250
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaAuditing(auditorAwareRef = "auditorAware")
|
||||
@EnableMethodCache(basePackages = "org.nl")
|
||||
@@ -37,6 +37,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
public class AppRun {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
SpringApplication.run(AppRun.class, args);
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.nl.annotation;
|
||||
package org.nl.modules.common.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
|
||||
@@ -1,29 +1,15 @@
|
||||
package org.nl.start;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.modules.mnt.util.DataTypeEnum;
|
||||
import org.nl.modules.quartz.domain.QuartzJob;
|
||||
import org.nl.modules.quartz.repository.QuartzJobRepository;
|
||||
import org.nl.modules.quartz.utils.QuartzManage;
|
||||
import org.nl.modules.system.service.impl.ParamServiceImpl;
|
||||
import org.nl.wql.WQLCore;
|
||||
import org.nl.wql.util.SpringContextHolder;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -32,143 +18,18 @@ import java.util.List;
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class Init implements ApplicationRunner, BeanFactoryAware, ApplicationContextAware {
|
||||
public class Init implements ApplicationRunner {
|
||||
private final QuartzJobRepository quartzJobRepository;
|
||||
private final QuartzManage quartzManage;
|
||||
|
||||
private final DefaultListableBeanFactory beanFactory;
|
||||
|
||||
|
||||
private void init() throws Exception {
|
||||
//初始化WQL
|
||||
initWql();
|
||||
//初始化任务调度
|
||||
initQuartz();
|
||||
//用户岗位表【sys_users_roles】
|
||||
System.out.println("项目启动成功!");
|
||||
}
|
||||
private void initOracle(){
|
||||
String jdbcUrl = "jdbc:oracle:thin:@10.16.1.30:1521:xrrun";
|
||||
String userName = "erp";
|
||||
String password = "erp";
|
||||
|
||||
DruidDataSource druidDataSource = new DruidDataSource();
|
||||
String className;
|
||||
try {
|
||||
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Get class name error: =" + jdbcUrl);
|
||||
}
|
||||
if (StrUtil.isEmpty(className)) {
|
||||
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
||||
if (null == dataTypeEnum) {
|
||||
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
||||
}
|
||||
druidDataSource.setDriverClassName(dataTypeEnum.getDriver());
|
||||
} else {
|
||||
druidDataSource.setDriverClassName(className);
|
||||
}
|
||||
|
||||
|
||||
druidDataSource.setUrl(jdbcUrl);
|
||||
druidDataSource.setUsername(userName);
|
||||
druidDataSource.setPassword(password);
|
||||
// 配置获取连接等待超时的时间
|
||||
druidDataSource.setMaxWait(3000);
|
||||
// 配置初始化大小、最小、最大
|
||||
druidDataSource.setInitialSize(5);
|
||||
druidDataSource.setMinIdle(5);
|
||||
druidDataSource.setMaxActive(10);
|
||||
|
||||
// 如果链接出现异常则直接判定为失败而不是一直重试
|
||||
druidDataSource.setBreakAfterAcquireFailure(true);
|
||||
try {
|
||||
druidDataSource.init();
|
||||
} catch (SQLException e) {
|
||||
log.error("Exception during pool initialization", e);
|
||||
throw new RuntimeException(e.getMessage());
|
||||
}
|
||||
System.out.println("oracle连接成功");
|
||||
|
||||
}
|
||||
|
||||
private void initDataSource1() {
|
||||
String IS_CONNECT_ERP = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("IS_CON_ERP_ORACLE").getValue();
|
||||
if (!StrUtil.equals("1", IS_CONNECT_ERP)) return;
|
||||
String jdbcUrl = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_ORACLE_URL").getValue();
|
||||
String userName = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_ORACLE_USER").getValue();
|
||||
String password = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_ORACLE_PWD").getValue();
|
||||
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DruidDataSource.class);
|
||||
String className;
|
||||
try {
|
||||
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Get class name error: =" + jdbcUrl);
|
||||
}
|
||||
if (StrUtil.isEmpty(className)) {
|
||||
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
||||
if (null == dataTypeEnum) {
|
||||
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
||||
}
|
||||
builder.addPropertyValue("driverClassName", dataTypeEnum.getDriver());
|
||||
} else {
|
||||
builder.addPropertyValue("driverClassName", className);
|
||||
}
|
||||
|
||||
builder.addPropertyValue("url", jdbcUrl);
|
||||
builder.addPropertyValue("username", userName);
|
||||
builder.addPropertyValue("password", password);
|
||||
// 配置获取连接等待超时的时间
|
||||
builder.addPropertyValue("maxWait", "3000");
|
||||
// 配置初始化大小、最小、最大
|
||||
builder.addPropertyValue("initialSize", "5");
|
||||
builder.addPropertyValue("minIdle", "5");
|
||||
builder.addPropertyValue("maxActive", "10");
|
||||
// 如果链接出现异常则直接判定为失败而不是一直重试
|
||||
builder.addPropertyValue("breakAfterAcquireFailure", "true");
|
||||
|
||||
beanFactory.registerBeanDefinition("dataSource1", builder.getBeanDefinition());
|
||||
}
|
||||
|
||||
public void initDataSource2() {
|
||||
String IS_CONNECT_ERP = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("IS_CON_ERP_SQLSERVER").getValue();
|
||||
if (!StrUtil.equals("1", IS_CONNECT_ERP)) return;
|
||||
String jdbcUrl = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_SQLSERVER_URL").getValue();
|
||||
String userName = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_SQLSERVER_USER").getValue();
|
||||
String password = SpringContextHolder.getBean(ParamServiceImpl.class).findByCode("ERP_SQLSERVER_PWD").getValue();
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(DruidDataSource.class);
|
||||
|
||||
String className;
|
||||
try {
|
||||
className = DriverManager.getDriver(jdbcUrl.trim()).getClass().getName();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Get class name error: =" + jdbcUrl);
|
||||
}
|
||||
if (StrUtil.isEmpty(className)) {
|
||||
DataTypeEnum dataTypeEnum = DataTypeEnum.urlOf(jdbcUrl);
|
||||
if (null == dataTypeEnum) {
|
||||
throw new RuntimeException("Not supported data type: jdbcUrl=" + jdbcUrl);
|
||||
}
|
||||
builder.addPropertyValue("driverClassName", dataTypeEnum.getDriver());
|
||||
} else {
|
||||
builder.addPropertyValue("driverClassName", className);
|
||||
}
|
||||
|
||||
builder.addPropertyValue("url", jdbcUrl);
|
||||
builder.addPropertyValue("username", userName);
|
||||
builder.addPropertyValue("password", password);
|
||||
// 配置获取连接等待超时的时间
|
||||
builder.addPropertyValue("maxWait", "3000");
|
||||
// 配置初始化大小、最小、最大
|
||||
builder.addPropertyValue("initialSize", "5");
|
||||
builder.addPropertyValue("minIdle", "5");
|
||||
builder.addPropertyValue("maxActive", "10");
|
||||
// 如果链接出现异常则直接判定为失败而不是一直重试
|
||||
builder.addPropertyValue("breakAfterAcquireFailure", "true");
|
||||
beanFactory.registerBeanDefinition("dataSource2", builder.getBeanDefinition());
|
||||
}
|
||||
|
||||
|
||||
private void initQuartz() {
|
||||
log.info("--------------------注入定时任务---------------------");
|
||||
@@ -188,14 +49,4 @@ public class Init implements ApplicationRunner, BeanFactoryAware, ApplicationCon
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
this.init();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory1) throws BeansException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user