简体   繁体   中英

Spring Boot - How to get the port at runtime without using spring annotations

I have a generic java agent based on aspectj which runs on several spring boot applications (and several spring boot versions). I'm trying to find a way to get the port on runtime, programatically when the application STARTS (before a request is made). The problem -

  1. I cannot use the regular way to get a spring boot port such as the ones described in https://stackoverflow.com/questions/30312058/spring-boot-how-to-get-the-running-port which includes @Autowired annotation etc.

  2. I'm looking for the port after the applications starts, without relying on requests to be made.

I'm looking for a way to access the LocalServerPort object or any other options that will allow me to access the port.

thanks for any help

After some investigation, as I mentioned I'm working with aspectj,I used the following instrumentation:

 after():
         execution(org.springframework.boot.context.embedded.EmbeddedServletContainer+.new(..))

And took the port directly from the EmbeddedServletContainer instance using the getPort() method. In case of Tomcat, if the port is -1, I use the embedded tomcat object to get the port from the connector itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM