Spring Boot - 不覆盖服务器端口属性

4

我有一个Spring Boot项目,其中服务器端口始终设置为8080,无论server.port属性如何。除server.port以外的所有属性都正确覆盖。属性配置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;
}

这意味着我的 classpath application.properties 是默认的(dev 属性),会被 jvm 参数 -Dproperties.location=C:\application\config 覆盖。
在我的 classpath 属性文件中未定义 server.port 属性,因此在 dev 环境中默认为 8080。这很好,但是对于测试,我想指定端口。我的外部属性文件包含此属性:
server.port=10070

日志:

[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

1
你是在Eclipse中运行你的项目吗?请注意,Eclipse运行配置会从类路径中排除application.properties文件(以及其他一些文件),这些文件通常位于Spring Boot项目的资源文件夹中。请查看项目->属性->Java Build Path Source。如果在命令行中也出现了这种情况,那么只需以调试模式运行应用程序(--debug),你就可以看到实际选择了哪些属性文件。 - cxubrix
为什么要自己折腾配置?首先应该是静态的。但是为什么不将所有内容直接添加到“application.properties”中或者使用Spring Boot的默认机制呢?(它已经支持你所做的)。与框架合作而非对立地工作。 - M. Deinum
@M.Deinum 我移除了自己的解决方案,改用更简单的 Boot 默认方式(在 /config 子文件夹中使用 application.properties)。一开始它没有起作用,这就是我自己开发解决方案的原因,但现在我已经让它工作了。谢谢。 - Jesper N
2个回答

9
如果您正在项目中使用Spring Actuator,默认情况下它指向8080端口,如果您想要更改端口,则在application.properties文件中添加“management.port = 9001”即可。

您还可以通过编程方式自定义端口。请按照以下链接操作:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-configure-tomcat - Pankaj Pandey
谢谢,我忘记了我已经将执行器添加到我的项目中,因为我还没有对其进行操作。添加 management.port 有效。 - Jesper N

1
您还可以尝试在运行时使用 -Dserver.port= 或 --server.port= 覆盖端口。当您以 java -jar 运行时使用后者。希望这有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接