4
votes

I've got a Spring Boot project where the server port always gets set to 8080, regardless of the server.port property. All properties except server.port gets overridden correctly. Property configuration bean:

@Bean
public PropertySourcesPlaceholderConfigurer properties() {
    final PropertySourcesPlaceholderConfigurer poc = new PropertySourcesPlaceholderConfigurer();
    poc.setIgnoreResourceNotFound(true);
    poc.setIgnoreUnresolvablePlaceholders(true);

    final List<Resource> list = new ArrayList<Resource>();

    // default (dev) properties
    list.add(new ClassPathResource(PROPERTIES_FILE));

    // override with -Dproperties.location=C:/path/to/properties/ where overriding application.properties resides
    list.add(new FileSystemResource(System.getProperty(EXTERNAL_ARGUMENT_NAME)+PROPERTIES_FILE));

    poc.setLocations(list.toArray(new Resource[]{}));

    return poc;
}

This means my classpath application.properties is the default (dev properties), which get overridden by jvm argument -Dproperties.location=C:\application\config.

The server.port property is not defined in my classpath properties file, so it defaults to 8080 in dev environment. This is fine, but for test I would like to specify the port. My external properties file contains this property:

server.port=10070

Logs:

[2016-01-19 11:14:10:010 CET]  INFO [restartedMain] support.PropertySourcesPlaceholderConfigurer: Loading properties file from class path resource [application.properties]
[2016-01-19 11:14:10:010 CET]  INFO [restartedMain] support.PropertySourcesPlaceholderConfigurer: Loading properties file from file [C:\var\opt\application\config\application.properties]
[2016-01-19 11:14:11:011 CET]  INFO [restartedMain] support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker: Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$418ca8e8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[2016-01-19 11:14:11:011 CET]  INFO [restartedMain] tomcat.TomcatEmbeddedServletContainer: Tomcat initialized with port(s): 8080 (http)
[2016-01-19 11:14:11:011 CET]  INFO [restartedMain] core.StandardService: Starting service Tomcat
2
Are you running your project from eclipse? Note that eclipse run config excludes applictation.properties files(and some others) from classpath. which are located in resources folder for default spring boot project. See project->properties->Java Build Path Source If this happens from command line as well then just run the application in debug mode( --debug) and you will see which properies files are actually picked up - cxubrix
Why are you hacking around with your own, configuration? First it should be static. But still why not simply add everything to application.properties or use the default spring boot mechanisms? (Which already support what you do). Work with instead of against the framework. - M. Deinum
@M.Deinum I removed my home made solution and went with the far easier Boot default (application.properties in /config sub folder). It didn't work at first, which is why I made my own solution, but now I got it working. Thanks. - Jesper N

2 Answers

8
votes

If you are using spring actuator in your project, by default it points to 8080, and if you want to change it, then in application.properties mention management.port = 9001

1
votes

You can also try overriding the port at runtime with -Dserver.port= or --server.port=. Use later when you are running as java -jar. Hope this helps.