Take a look at this for how to configure a port: Spring Boot - how to configure port
Relevant code is this:
@Controller
public class ServletConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return (container -> {
container.setPort(8012);
});
}
In general, most properties that can be configured via application.properties can also be configured through a Java bean. But, I would suggest using application.properties
if you can. It allows you to change properties, without having to change source code.
EDIT:
Some other code from the posted link you might find useful:
HashMap<String, Object> props = new HashMap<>();
props.put("server.port", 9999);
new SpringApplicationBuilder()
.sources(SampleController.class)
.properties(props)
.run(args);