New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relocate @Local*Port to spring-boot-test #29589
Comments
Configuration properties are resolved and bound from the environment before the application starts. To get a random port from the OS, you need to start the application and the server, using the configuration properties you've provided. This is by design and I'm closing this issue as a result. |
It would be nice if we could get @mihaelaDev You can use an event listener to grab the port once the web server has started. Depending on what you're trying to do this might work, but be aware that they even fires after beans have been created. @EventListener
void onWebInit(WebServerInitializedEvent event) {
int port = event.getWebServer().getPort();
} |
|
It's an |
We're going to relocate the |
We have three annotations to move:
I'm not keen on an
|
Maybe it was not clear that my configuration class is not productive code. I need the
I want to start a
Please find attached a project example with my use case. |
@mihaelaDev The annotation can only be used for injection into something that's created after the web server has been started which happens right at the end of application context creation. Typically, this means injection directly into the test class itself. Your One way to get this to work as you'd like is to defer the creation of your @Bean
@Lazy
CXFSoapServiceMyService testBean(@LocalServerPort int randomServerPort) {
final String address = String.format("http://localhost:%d/MyService", randomServerPort);
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress(address);
return factoryBean.create(CXFSoapServiceMyService.class);
} In your sample at least, this defers the need for |
Now it's clear. Thank you @wilkinsona ! I'll use your suggestion. |
Excellent work |
From a Spring Boot test I am starting the embedded Tomcat on a random port:
Inside the test scope, I have a configuration file in which I need to get the random port:
When I run the test I get an
IllegalStateException
:If I use
ServletWebServerApplicationContext
, or@Value("${server.port}")
to get the port, everything is working fine.Spring Boot version: 2.6.3
With Apache CXF version 3.5.0
The text was updated successfully, but these errors were encountered: