温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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