The spring boot process executes a function (four solutions)
fhspringcloud 2021-07-20 04:31:07

 1、 Realization ServletContextAware Interface and rewrite it setServletContext Method

@Component
public class TestStarted implements ServletContextAware {
/**
* Fill in the normal bean Property, but is called before initialization.
* Be similar to initializingbean Of afterpropertiesset Or custom init Method's callback
*
*/
@Override
public void setServletContext(ServletContext servletContext) {
System.out.println("setServletContext Method ");
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Be careful : This method will fill in the normal Bean Properties of , But not yet Bean Before initialization of

2、 Realization ServletContextListener Interface

/**java project www.fhadmin.org
* Initializing Web Any filter in the application or servlet Before , Will notify all servletContextListener Context initialization .
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
//ServletContext servletContext = sce.getServletContext();
System.out.println(" perform contextInitialized Method ");
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

3、 Intersect the class of the method to be executed spring Container scan (@Component), And add the @PostConstruct Annotation or static block execution

//java project www.fhadmin.org
@Component
public class Test2 {
// Static code blocks execute automatically after dependency injection , And give priority to implementation
static{
System.out.println("---static--");
}
/**
* @Postcontruct’ Called automatically after dependency injection is complete
*/
@PostConstruct
public static void haha(){
System.out.println("@Postcontruct’ Called automatically after dependency injection is complete ");
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

4、 Realization ApplicationRunner Interface

/**java project www.fhadmin.org
* Used to indicate bean Included in SpringApplication Interface to run when in . Multiple can be defined applicationrunner bean
* In the same application context , You can use ordered interfaces or @order Notes sort them .
*/
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner Of run Method ");
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

5、 Realization CommandLineRunner Interface

/**
* Used to indicate bean Included in SpringApplication Interface to run when in . Multiple can be defined in the same application context commandlinerunner bean, And you can use ordered interfaces or @order Notes sort them .
* If you need to visit applicationArguments Instead of the original string array , Please consider using applicationrunner.
* java project www.fhadmin.org
*/
@Override
public void run(String... ) throws Exception {
System.out.println("CommandLineRunner Of run Method ");
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.



Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09