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接口均在项目启动日志打印完毕之后才执行。而且这几种方式打印的顺序也不会发生改变。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

luffy5459

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

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

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

打赏作者

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

抵扣说明:

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

余额充值