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