SpringBoot项目启动时如何读取配置以及初始化资源

作者:Andya_net 时间:2021-11-19 04:04:11 

介绍

  在开发过程中,我们有时候会遇到非接口调用而出发程序执行任务的一些场景,比如我们使用quartz定时框架通过配置文件来启动定时任务时,或者一些初始化资源场景等触发的任务执行场景。

方法一:注解

方案

  通过使用注解@Configuration和@Bean来初始化资源,配置文件当然还是通过@Value进行注入。

  • @Configuration:用于定义配置类,可替换xml配置文件,被注解的类内部一般是包含了一个或者多个@Bean注解的方法。

  • @Bean:产生一个Bean对象,然后将Bean对象交给Spring管理,被注解的方法是会被AnnotationConfigApplicationContext或者AnnotationConfgWebApplicationContext扫描,用于构建bean定义,从而初始化Spring容器。产生这个对象的方法Spring只会调用一次,之后Spring就会将这个Bean对象放入自己的Ioc容器中。

补充@Configuration加载Spring:

  1. @Configuration配置spring并启动spring容器

  2. @Configuration启动容器+@Bean注册Bean

  3. @Configuration启动容器+@Component注册Bean

  4. 使用 AnnotationConfigApplicationContext 注册 AppContext 类的两种方法

  5. 配置Web应用程序(web.xml中配置AnnotationConfigApplicationContext)

示例


package com.example.andya.demo.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author andya
* @create 2020-06-24 14:37
*/
@Configuration
public class InitConfigTest {

@Value("${key}")
private String key;

@Bean
public String testInit(){
 System.out.println("init key: " + key);
 return key;
}
}

方法二:CommandLineRunner

方案

  实现CommandLineRunner接口,该接口中的Component会在所有Spring的Beans都初始化之后,在SpringApplication的run()之前执行。

  多个类需要有顺序的初始化资源时,我们还可以通过类注解@Order(n)进行优先级控制

示例


package com.example.andya.demo.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
* @author andya
* @create 2020-06-24 14:47
*/
@Component
public class CommandLineRunnerTest implements CommandLineRunner {

@Value("${key}")
private String key;

@Override
public void run(String... strings) throws Exception {
 System.out.println("command line runner, init key: " + key);
}
}

两个示例的运行结果

SpringBoot项目启动时如何读取配置以及初始化资源

来源:https://www.cnblogs.com/Andya/p/13187845.html

标签:springboot,配置,初始化
0
投稿

猜你喜欢

  • Java中值类型和引用类型详解

    2023-11-29 00:44:51
  • Java类的定义以及执行顺序学习教程

    2023-01-14 10:44:29
  • Mybatis利用OGNL表达式处理动态sql的方法教程

    2022-11-26 22:22:02
  • 详细总结Java堆栈内存、堆外内存、零拷贝浅析与代码实现

    2023-11-21 01:52:29
  • java实现随机数生成器

    2023-06-15 04:26:53
  • 一文教会你使用jmap和MAT进行堆内存溢出分析

    2023-11-06 08:01:57
  • java开源项目jeecgboot的超详细解析

    2023-07-19 03:30:53
  • Springboot+SpringSecurity+JWT实现用户登录和权限认证示例

    2021-11-14 11:06:11
  • Java 继承与多态超详细梳理

    2023-11-26 09:01:08
  • C#根据年月日计算星期几的函数

    2022-03-17 04:50:50
  • java实现波雷费密码算法示例代码

    2022-09-14 23:30:28
  • Java的动态绑定与双分派_动力节点Java学院整理

    2021-07-14 11:18:50
  • JAVA Spring中让人头痛的JAVA大事务问题要如何解决你知道吗

    2023-01-14 04:58:04
  • vs2019永久配置opencv开发环境的方法步骤

    2023-11-02 19:38:58
  • Spring JDK动态 代理实现过程详解

    2023-11-16 19:42:15
  • Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 实现分库分表功能

    2023-08-28 16:52:09
  • C#实现类似jQuery的方法连缀功能

    2022-04-04 04:30:50
  • Java监听器ActionListener与MouseListener的执行顺序说明

    2022-02-04 20:08:23
  • 图文详解Maven工程打jar包的N种方式

    2022-12-12 21:36:23
  • IntelliJ IDEAx导出安卓(Android)apk文件图文教程

    2022-06-22 18:26:16
  • asp之家 软件编程 m.aspxhome.com