SpingBoot项目启动时执行一次代码(全)

有时根据项目要求需要在项目启动时去执行一些操作,比如资源的加载、系统IP的获取、 读取配置文件,数据库连接等,实现方法也有多种。

一、java自身加载方式实现

static静态代码块:

static关键字所修饰的内容在整个类执行前优先加载

构造方法:

在对象初始化时执行,执行顺序在静态代码块之后

@Component
public class Test {
    static{
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("IP Address1 : " + ip.getHostAddress());
    }
    public Test(){
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("IP Address2 : " + ip.getHostAddress());
    }
}

执行结果:

在这里插入图片描述

二、@PostConstruct注解

@PostConstruct是Java自带的注解,在方法上加该注解会在项目启动的时候执行该方法,在spring容器初始化的时候执行该方法,执行顺序在静态代码块和构造方法之后。

@Component
public class PostConstructTest {
    static{
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("IP Address1 : " + ip.getHostAddress());
    }
    public PostConstructTest(){
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("IP Address2 : " + ip.getHostAddress());
    }
    @PostConstruct
    public void init(){
        InetAddress ip = null;
        try {
            ip = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println("IP Address3 : " + ip.getHostAddress());
    }
}

执行结果:
在这里插入图片描述

三、CommandLineRunner、ApplicationRunner接口

SpringBoot提供了两个启动加载接口分别是:CommandLineRunner和ApplicationRunner。

在applicationContext容器加载完成之后,会调用SpringApplication类的callRunners方法:该方法中会获取所有实现了ApplicationRunner和CommandLineRunner的接口bean,然后依次执行对应的run方法,并且是在同一个线程中执行。

CommandLineRunner
@Component
@Order(value = 1)
public class CommandLineRunnerTest1 implements CommandLineRunner{

    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunnerTest1");
    }
}
@Component
@Order(value = 2)
public class CommandLineRunnerTest2 implements CommandLineRunner{

    @Override
    public void run(String... args) throws Exception {
        System.out.println("CommandLineRunnerTest2");
    }
}
@Component
@Order(value = 1)
public class ApplicationRunnerTest1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunnerTest1");
    }
}
@Component
@Order(value = 2)
public class ApplicationRunnerTest2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunnerTest2");
    }
}

在这里插入图片描述
在这里插入图片描述

有多个类实现了CommandLineRunner、ApplicationRunner接口时,可使用@Order(1)注解来控制执行顺序,value值越小、越先执行@Order(1)>@Order(2),ApplicationRunner执行顺序在CommandLineRunner之前

四、InitializingBean接口

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法 。

public class InitializingBeanTest implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
    System.out.println("InitializingBeanTest");
}

在这里插入图片描述

以上四种方式都可实现在项目启动后执行一次方法。执行顺序如下:
s t a t i c 静态方法 > 构造方法 > @ P o s t C o n s t r u c t 注解 > I n i t i a l i z i n g B e a n > A p p l i c a t i o n R u n n e r > C o m m a n d L i n e R u n n e r static静态方法>构造方法>@PostConstruct注解>InitializingBean>ApplicationRunner>CommandLineRunner static静态方法>构造方法>@PostConstruct注解>InitializingBean>ApplicationRunner>CommandLineRunner
其中ApplicationRunner:@Order(1)>CommandLineRunner:@Order(1)>ApplicationRunner:@Order(2)>CommandLineRunner:@Order(2)

@Component
public class PostConstructTest {
    static {
        System.out.println("static静态方法");
    }

    public PostConstructTest() {
        System.out.println("构造方法");
    }

    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct注解");
    }

    @Component
    @Order(value = 1)
    public class CommandLineRunnerTest1 implements CommandLineRunner {

        @Override
        public void run(String... args) throws Exception {
            System.out.println("CommandLineRunner:@Order(1)");
        }
    }

    @Component
    @Order(value = 2)
    public class CommandLineRunnerTest2 implements CommandLineRunner {

        @Override
        public void run(String... args) throws Exception {
            System.out.println("CommandLineRunner:@Order(2)");
        }
    }

    @Component
    @Order(value = 1)
    public class ApplicationRunnerTest1 implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("ApplicationRunner:@Order(1)");
        }
    }

    @Component
    @Order(value = 2)
    public class ApplicationRunnerTest2 implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("ApplicationRunner:@Order(2)");
        }
    }

    @Component
    public class InitializingBeanTest implements InitializingBean {

        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("InitializingBean");
        }
    }
}

在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot项目中,可以使用初始化代码来实现项目启动时的一些初始化操作。下面以示例代码进行解释: 1. 创建一个类,命名为ApplicationRunnerImpl实现ApplicationRunner接口,该接口继承了CommandLineRunner接口,用于在Spring Boot项目启动之后执行特定的代码。 ```java import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { // 这里写入初始化代码,例如加载配置、数据库连接等操作 System.out.println("Spring Boot项目启动初始化代码执行!"); } } ``` 2. 在启动类中,使用@SpringBootApplication注解启动Spring Boot应用程序。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 在上述示例代码中,当Spring Boot项目启动之后,ApplicationRunnerImpl类中的run方法会被执行,可以在此方法中编写一些初始化的代码。比如加载配置文件、初始化数据库连接等。上面的例子中,run方法内只打印了一条信息。实际应用中,可以根据需要编写具体的初始化逻辑。 当代码执行时,控制台会打印出"Spring Boot项目启动初始化代码执行!"的信息,表示初始化代码成功执行。 这种方式非常适用于需要在项目启动时执行一些初始化操作的场景,可以方便地集成到Spring Boot框架中,实现项目的自动初始化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值