springboot自定义stater启动流程
作者:好歹取个名字 时间:2023-06-07 06:33:45
springboot启动时自动加载application.properties或者application.yml,如何定义自己的配置让springboot自动识别:
首先我们新建一个maven工程打包方式选择jar,然后引入所需的包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.ruobbo.xxl</groupId>
<artifactId>ruobbo.xxl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<!-- 项目部署配置 (上传到 * 的配置) -->
<distributionManagement>
<repository>
<id>releases</id>
<url>http://192.168.1.99:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.1.99:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</project>
然后是配置读取
@ConfigurationProperties(prefix = "xxl.job.executor")
@Data
public class XxlProperties {
private String adminAddresses;
private String appName;
private String ip;
private int port;
private String accessToken;
private String logPath;
private int logRetentionDays;
private String basePackages;
}
将读取到的配置注入到bean中,然后加载到spring bean容器中
@Configuration
@EnableConfigurationProperties(XxlProperties.class)
public class XxlAutoConfiguration {
private Logger logger = LoggerFactory.getLogger(XxlAutoConfiguration.class);
@Resource
private XxlProperties xxlProperties;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(xxlProperties.getAdminAddresses());
xxlJobSpringExecutor.setAppName(xxlProperties.getAppName());
xxlJobSpringExecutor.setIp(xxlProperties.getIp());
xxlJobSpringExecutor.setPort(xxlProperties.getPort());
xxlJobSpringExecutor.setAccessToken(xxlProperties.getAccessToken());
xxlJobSpringExecutor.setLogPath(xxlProperties.getLogPath());
xxlJobSpringExecutor.setLogRetentionDays(xxlProperties.getLogRetentionDays());
return xxlJobSpringExecutor;
}
}
最后在resources目录下添加META-INF文件夹添加spring.factories文件,文件内容为指定要初始化springbean的类全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.ruobbo.xxl.XxlAutoConfiguration
使用过程中引入包
<dependency>
<groupId>com.ruobbo.xxl</groupId>
<artifactId>ruobbo.xxl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
然后添加配置到application.properties或者application.yml
总结
以上所述是小编给大家介绍的springboot自定义stater启动流程网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/qq_30920479/article/details/102636699
标签:springboot,自定义,stater,启动
0
投稿
猜你喜欢
C#中结构体定义并转换字节数组详解
2023-01-23 11:07:50
Android实现背景颜色滑动渐变效果的全过程
2021-08-28 09:23:51
Android之Spinner用法详解
2022-10-05 00:57:17
C#设置输入法实例分析
2022-07-07 14:30:05
浅谈Java自定义类加载器及JVM自带的类加载器之间的交互关系
2021-09-12 23:37:24
springmvc @RequestBody String类型参数的使用
2023-03-08 09:30:05
关于mybatis resulttype 返回值异常的问题
2021-08-09 20:26:19
C语言中的各种文件读写方法小结
2023-06-13 02:44:40
Java设计模式之状态模式
2022-05-08 07:24:25
Java中Spring Boot+Socket实现与html页面的长连接实例详解
2022-12-24 12:06:12
C#判断字符串中是否包含指定字符串及contains与indexof方法效率问题
2022-07-06 19:04:51
Android自定义TextView跑马灯效果
2023-08-07 01:14:57
Spring Boot + Mybatis多数据源和动态数据源配置方法
2023-02-16 17:15:31
完美解决虚拟按键遮盖底部视图的问题
2021-11-24 23:48:25
SpringBoot整合Mybatis自定义拦截器不起作用的处理方案
2023-03-19 04:35:14
Android实现雷达View效果的示例代码
2022-09-27 11:39:52
Android 后台发送邮件示例 (收集应用异常信息+Demo代码)
2022-06-24 16:31:06
Java JDK与cglib动态代理有什么区别
2023-07-23 08:10:15
自定义时间格式转换代码分享
2022-11-03 03:09:23
c#图片缩放图片剪切功能实现(等比缩放)
2022-08-17 04:46:13