This article takes spring boot and automatically opens a browser to access the HTML 5 page of the project to demonstrate
Introduce two methods and recommend the second one
The first is to manually add monitoring tasks to spring, which is troublesome
Step 1: write a thread class first. After the springboot is started and loaded, the automatic operation will be placed in it
package com.sinosoft.speech.util; import org.springframework.stereotype.Component; /** * Actions to be performed * @author lijunming * @date 2018/7/22 2:54 p.m. */ @Component public class StepExecutor implements Runnable { @Override public void run() { startStreamTask(); } /** * Open 1 page after project startup */ public void startStreamTask() { System.out.println("Start 1 startup page"); try { Runtime.getRuntime().exec("cmd /c start http://localhost:8080/index"); } catch (Exception ex) { ex.printStackTrace(); } } }
Step 2: write a listener and perform the specified operation after the listening items are loaded
package com.sinosoft.speech.util; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; /** * spring boot Execute after container loading * @author lijunming * @date 2018/7/22 3:04 p.m. */ public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { ApplicationContext ac = event.getApplicationContext(); StepExecutor StepExecutor = ac.getBean(StepExecutor .class); Thread thread = new Thread(StepExecutor); thread.start(); } }
Step 3: add a listening task to the springboot startup class
package com.sinosoft.speech; import com.sinosoft.speech.util.ApplicationStartup; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author lijunming * @date 2018/7/22 2:40 p.m. */ @SpringBootApplication public class SpeechApplication { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(SpeechApplication .class); springApplication.addListeners(new ApplicationStartup()); springApplication.run(args); } }
The second kind of spring container automatically listens after loading
Step 1: implement the default listening interface of springboot, which automatically listens after the spring container is loaded
package com.sinosoft.speech.util; import com.sinosoft.speech.swing.Main; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; /** * spring boot Automatic monitoring after container loading * @author lijunming * @date 2018/7/22 2:30 p.m. */ @Component public class MyCommandRunner implements CommandLineRunner { @Override public void run(String... args) { System.out.println("Start 1 page"); try { Runtime.getRuntime().exec("cmd /c start http://localhost:8080/index"); } catch (Exception ex) { ex.printStackTrace(); } } }
2. Just start normally
package com.sinosoft.speech; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author lijunming * @date 2018/7/22 2:40 p.m. */ @SpringBootApplication public class SpeechApplication { public static void main(String[] args) { SpringApplication.run(SpeechApplication.class, args); } }
After configuration, run the springboot startup class, and a browser will automatically open to access the html5 page of the project. The effect is as follows: