中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot自定義stater啟動流程

發布時間:2020-09-09 14:38:45 來源:腳本之家 閱讀:199 作者:好歹取個名字 欄目:編程語言

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

springboot自定義stater啟動流程

使用過程中引入包

 

   <dependency>
      <groupId>com.ruobbo.xxl</groupId>
      <artifactId>ruobbo.xxl</artifactId>
      <version>0.0.1-SNAPSHOT</version>
    </dependency>

然后添加配置到application.properties或者application.yml

springboot自定義stater啟動流程

總結

以上所述是小編給大家介紹的springboot自定義stater啟動流程,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

寿阳县| 会宁县| 韶山市| 平泉县| 当阳市| 云梦县| 高碑店市| 忻州市| 庄浪县| 汨罗市| 莒南县| 太和县| 军事| 弋阳县| 察隅县| 辉县市| 青海省| 闸北区| 临高县| 柳河县| 洪湖市| 宜良县| 隆子县| 维西| 鄂伦春自治旗| 安阳市| 鹰潭市| 柘城县| 天长市| 清流县| 隆尧县| 青冈县| 舟曲县| 蓬溪县| 奉贤区| 林芝县| 马鞍山市| 互助| 仪征市| 泽库县| 盐山县|