您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何使用springboot自定義stater啟動流程,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
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> <!-- 項(xiàng)目部署配置 (上傳到私服的配置) --> <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")@Datapublic 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文件,文件內(nèi)容為指定要初始化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
關(guān)于“如何使用springboot自定義stater啟動流程”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。