After the springboot configuration project is started, the browser is automatically opened to access the project
Sometimes, when the project is deployed on a single machine, or the project is not running in the idea development tool (idea can automatically open the tomcat project), you need to automatically open the browser to access the project after the project is started. The configuration method is very simple.
1, Specific steps
The only code used is the following sentence, that is, cmd calls a browser to open the page. I call google's browser program to open the project.
String cmd = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe http://localhost:8080"; Runtime run = Runtime.getRuntime(); try{ run.exec(cmd); logger.debug("Start the browser to open the project successfully"); }catch (Exception e){ e.printStackTrace(); logger.error(e.getMessage()); }
2, Where is it written
Some people say to add a filter. I don't quite understand. In fact, we only need to automatically open the browser and access the project once after the project is started. If the filter is used, how can we do it only once?
So I think we should add a class. That class can only be executed to open the browser after the project is started. As we all know, the startup items of the springboot project are as follows:
@SpringBootApplication public class SpringbootQqAction { public static void main(String[] args){ SpringApplication.run(SpringbootQqAction.class,args); //Can you add it here? } }
As above, can you put the execution steps of starting the browser on top? It's OK after my test. Because the SpringApplication.run method is synchronously blocked, only after it is completed can the subsequent continue. There will be no problem of opening the browser to access the project before the project is started.
3, My realization
I didn't put it directly into the startup item as mentioned above. I think it's too inflexible. I want to make a switch so that these things can still be configured after packing.
We all know that when the spring project is started, there are many methods to let it execute the next thing, such as ServletContainerInitializer and the ServletContextListener I often use to listen to the container life cycle. Here I use another one, CommandLineRunner, which is the springboot itself, After the project is started, the run method in the CommandLineRunner implementation class will be automatically scanned and executed.
1. Create MyCommandRunner
This class can be placed one level lower than the springboot startup class. Generally, I will create a config folder and put them together. For example:
src --main ----java --------springboot.oa ---------------------config ---------------------------MyCommandRunner.java ---------------------SpringbootQqAction.java
The specific contents of MyCommandRunner class are as follows:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.awt.*; import java.net.URI; @Component public class MyCommandRunner implements CommandLineRunner { private static Logger logger = LoggerFactory.getLogger(MyCommandRunner.class); @Value("${spring.web.loginurl}") private String loginUrl; @Value("${spring.web.googleexcute}") private String googleExcutePath; @Value("${spring.auto.openurl}") private boolean isOpen; @Override public void run(String... args) throws Exception { if(isOpen){ String cmd = googleExcutePath +" "+ loginUrl; Runtime run = Runtime.getRuntime(); try{ run.exec(cmd); logger.debug("Start the browser to open the project successfully"); }catch (Exception e){ e.printStackTrace(); logger.error(e.getMessage()); } } } }
2. Configuration parameters
There are three variables loginUrl, googleExcutePath and ispen. The @ Value values corresponding to these three variables are configured in the springboot configuration file application.properties, as follows:
#Whether to open it. If you want to open it, write true below spring.auto.openurl=false spring.web.loginurl=http://localhost:8180 spring.web.googleexcute=C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe
Note that the access project path must start with http: / /. If you write localhost:8080, the access will not succeed.
3. Why
The advantage of this is that I can switch whether to open the browser to access the project after the project is started at any time, and I can configure the access path and default browser at any time. The most important thing is that during project deployment, we usually use jars or wars. If these things are written to the class, they cannot be changed directly. They can be modified at any time when they are written to the xml or application.properties file. This method supports spring boot running in jar and war.