springboot项目启动自动执行代码几种方案

    在spring或者springboot项目中,经常会有这样的需求,就是项目启动之后,会执行一段代码,做一些初始化的操作,执行完毕,就不再重复执行。

    在spring项目中,可以通过实现InitializingBean接口的方式来实现,另外,项目如果是springweb项目,那么可以通过实现ServletContextListener和ServletContextAware接口等来实现。

    在springboot项目中,可以通过实现ApplicationRunner接口和CommandLineRunner接口。

    还有一种方式,不用实现接口,通过注解@PostConstruct来实现。

    下面给出以上列举的6中方式实现代码:

    为了实现所有的方式,我们在maven工程中加入spring-boot-starter-web的依赖。

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.4.RELEASE</version>
</parent>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter</artifactId>
	</dependency>

	<!--  -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
</dependencies>

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<source>${java.version}</source>
				<target>${java.version}</target>
				<fork>true</fork>
				<addResources>true</addResources>
			</configuration>
		</plugin>
	</plugins>

</build>

    DemoServiceWithApplicationRunner.java

package com.xxx.service;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceWithApplicationRunner implements ApplicationRunner{

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("5: run with ApplicationRunner , method -> run().");
	}

}

     DemoServiceWithCommandLineRunner.java

package com.xxx.service;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceWithCommandLineRunner implements CommandLineRunner{

	@Override
	public void run(String... args) throws Exception {
		System.out.println("6: run with CommandLineRunner , method -> run().");
	}

}

    DemoServiceWithInitializingBean.java 

package com.xxx.service;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceWithInitializingBean implements InitializingBean{

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("2: run with InitializingBean , method -> afterPropertiesSet().");
	}
	
}

    DemoServiceWithPostConstruct.java 

package com.xxx.service;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceWithPostConstruct {
	@PostConstruct
	public void run() {
		System.out.println("3: run with PostConstruct , method -> run().");
	}
}

    DemoServiceWithServletContextAware.java 

package com.xxx.service;
import javax.servlet.ServletContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
@Component
public class DemoServiceWithServletContextAware implements ServletContextAware{

	@Override
	public void setServletContext(ServletContext servletContext) {
		System.out.println("4: run with ServletContextAware , method -> setServletContext().");
	}

}

    DemoServiceWithServletContextListener.java 

package com.xxx.service;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.stereotype.Component;
@Component
public class DemoServiceWithServletContextListener implements ServletContextListener{
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("1: run with ServletContextListener , method -> contextInitialized().");
	}
}

    App.java 

package com.xxx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
    public static void main( String[] args ){
    	SpringApplication.run(App.class, args);
    }
}

     运行代码,打印信息截图:

    可以看出的是,使用springboot提供的ApplicationRunner接口和CommandLineRunner接口均在项目启动日志打印完毕之后才执行。而且这几种方式打印的顺序也不会发生改变。

  • 11
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
在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框架中,实现项目自动初始化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffy5459

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值