springboot自定义stater启动流程

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启动流程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

(0)

相关推荐

  • Spring boot自定义http反馈状态码详解

    前言 最近在开发一些http server类型程序,通过spring boot构建一些web程序,这些web程序之间通过http进行数据访问.共享,如下图, 假设现在client发起一次保存数据的请求到server,server可能会返回如下类似的数据 { "status":1, "message":"xxxxxx" } 然后client通过解析json获得status来判断当前的请求操作是否成功,开发过程中通过都是这么做的,但是这样在restf

  • spring boot自定义log4j2日志文件的实例讲解

    背景:因为从 spring boot 1.4开始的版本就要用log4j2 了,支持的格式有json和xml两种格式,此次实践主要使用的是xml的格式定义日志说明. spring boot 1.5.8.RELEASE 引入log4j2的开发步骤如下: 1.首先把spring-boot-starter-web以及spring-boot-starter包下面的spring-boot-starter-logging排除,然后引入spring-boot-starter-log4j2包. <dependen

  • Springboot读取配置文件及自定义配置文件的方法

    1.创建maven工程,在pom文件中添加依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency

  • Spring Boot中整合Spring Security并自定义验证代码实例

    最终效果 1.实现页面访问权限限制 2.用户角色区分,并按照角色区分页面权限 3.实现在数据库中存储用户信息以及角色信息 4.自定义验证代码 效果如下: 1.免验证页面 2.登陆页面 在用户未登录时,访问任意有权限要求的页面都会自动跳转到登陆页面. 3.需登陆才能查看的页面 用户登陆后,可以正常访问页面资源,同时可以正确显示用户登录名: 4.用户有角色区分,可以指定部分页面只允许有相应用户角色的人使用 4.1.只有ADMIN觉得用户才能查看的页面(权限不足) 4.2.只有ADMIN觉得用户才能查

  • spring boot装载自定义yml文件

    yml格式的配置文件感觉很人性化,所以想把项目中的.properties都替换成.yml文件,主要springboot自1.5以后就把@configurationProperties中的location参数去掉,各种查询之后发现可以用YamlPropertySourceLoader去装载yml文件,上代码 public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ResourceLoader loade

  • 在SpringBoot下读取自定义properties配置文件的方法

    SpringBoot工程默认读取application.properties配置文件.如果需要自定义properties文件,如何读取呢? 一.在resource中新建.properties文件 在resource目录下新建一个config文件夹,然后新建一个.properties文件放在该文件夹下.如图remote.properties所示 二.编写配置文件 remote.uploadFilesUrl=/resource/files/ remote.uploadPicUrl=/resource

  • springboot自定义stater启动流程

    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

  • SpringBoot自定义Starter实现流程详解

    目录 starter起步依赖 starter命名规则 自定义starter new module 添加依赖 simplebean 自动配置类 META-INF\spring.factories 在spring-boot-mytest中引入mystarter-spring-boot-starter 添加配置 通过@Autowired引用 启动访问 starter起步依赖 starter起步依赖是springboot一种非常重要的机制, 它打包了某些场景下需要用到依赖,将其统一集成到starter,

  • SpringBoot自定义启动器Starter流程详解

    目录 一.背景 二.自定义启动器 1.创建一个启动器的自动配置模块 2.创建一个启动器模块 3.在业务模块中引入启动器 一.背景 虽然Spring官方给我们提供了很多的启动器供我们使用 但有时候我们也会遇到某些特殊场景,这些启动器满足不了 这个时候就需要自定义一个启动器供我们使用 二.自定义启动器 在之前学习Spring Boot的过程中,我们已经对启动器有了一个大致的了解 Spring Boot实现某个功能,一般是引入对应场景的启动器(一般不写代码,只是声明这个启动器需要引用哪些依赖),然后这

  • springboot中swagger快速启动流程

    介绍 可能大家都有用过swagger,可以通过ui页面显示接口信息,快速和前端进行联调. 没有接触的小伙伴可以参考官网文章进行了解下demo页面. 多应用 当然在单个应用大家可以配置SwaggerConfig类加载下buildDocket,就可以快速构建好swagger了. 代码大致如下: /** * Swagger2配置类 * 在与spring boot集成时,放在与Application.java同级的目录下. * 通过@Configuration注解,让Spring来加载该类配置. * 再

  • SpringBoot详解自定义Stater的应用

    目录 1.SpringBoot starter机制 2.为什么要自定义starter 3.自定义starter的命名规则 4.使用自定义starter 1.SpringBoot starter机制 SpringBoot由众多Starter组成(一系列的自动化配置的starter插件),SpringBoot之所以流行,也是因为starter.starter是SpringBoot非常重要的一部分,可以理解为一个可拔插式的插件,正是这些starter使得使用某个功能的开发者不需要关注各种依赖库的处理,

  • SpringBoot自定义路由覆盖实现流程详解

    目录 背景 设计 实现 注解定义 注解扫描及管理 自定义RequestMappingHandlerMapping 注册RequestMappingHandlerMapping 使用示例 背景 公司最近有一个项目二期需要对一些功能进行改造,涉及部分框架内置业务接口个性化定制,兼容老接口功能并且增加一部分新的数据返回,由于前端调用这些接口分布较多且较为零碎,修改测试成本较大,所以打算在框架层面提供路由覆盖功能,加快项目进度减少无技术含量的修改带来的系统风险 设计 提供自定义注解指定需要覆盖的路由及新

  • Spring Boot启动流程分析

    引言 早在15年的时候就开始用spring boot进行开发了,然而一直就只是用用,并没有深入去了解spring boot是以什么原理怎样工作的,说来也惭愧.今天让我们从spring boot启动开始,深入了解一下spring boot的工作原理. 为什么用spring boot 在使用一个东西或者一个工具之前,我们总是会问自己,我为什么要用?用他能给我带来什么好处? * 最大的好处就是spring boot遵从了java**约定大于配置**不用面对一大堆的配置文件,spring boot是根据

  • spring boot容器启动流程

    一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是 约定大于配置 ,但是原理呢?为什么要这么做?).spring cloud是基于spring boot快速搭建的,今天咱们就看看spring boot容器启动流程.(本文不讲解如何快速启动spring boot,那些直接官方看即可, 官网文档飞机票 ) 二.容器启动 spring boot一般是 指定容器启动main方法,然后以命令行方式启动Jar包 ,如下图: @SpringBootApplicati

  • springboot自定义redis-starter的实现

    spring时代整合redis spring我相信只要是一个Java开发人员我相信再熟悉不过了,几乎垄断了整个JavaEE的市场份额,话不多说进入正题. 首先看看我们在spring中整合redis需要做什么 1.首先maven工程的话不用想先导入依赖 <!-- jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> &

  • Spring体系的各种启动流程详解

    在介绍spring的启动之前,先来说下启动过程中使用到的几个类 基本组件 1.BeanFactory:spring底层容器,定义了最基本的容器功能,注意区分FactoryBean 2.ApplicationContext:扩展于BeanFactory,拥有更丰富的功能.例如:添加事件发布机制.父子级容器,一般都是直接使用ApplicationContext. 3.Resource:bean配置文件,一般为xml文件.可以理解为保存bean信息的文件. 4.BeanDefinition:beand

随机推荐