简体   繁体   English

Spring Boot-如何在运行时获取端口而不使用spring注释

[英]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). 我有一个基于Aspectj的通用Java代理,该代理在多个Spring Boot应用程序(和多个Spring Boot版本)上运行。 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. 我无法使用常规方式来获取Spring Boot端口,例如https://stackoverflow.com/questions/30312058/spring-boot-how-to-get-the-running-port中所述的端口,其中包括@Autowired注释等等

  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. 我正在寻找一种访问LocalServerPort对象或任何其他允许我访问端口的选项。

thanks for any help 谢谢你的帮助

After some investigation, as I mentioned I'm working with aspectj,I used the following instrumentation: 经过调查,正如我提到的,我正在使用Aspectj,我使用了以下工具:

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

And took the port directly from the EmbeddedServletContainer instance using the getPort() method. 并使用getPort()方法直接从EmbeddedServletContainer实例获取端口。 In case of Tomcat, if the port is -1, I use the embedded tomcat object to get the port from the connector itself. 对于Tomcat,如果端口为-1,则使用嵌入式tomcat对象从连接器本身获取端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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