Java Spring启动-不重写服务器端口属性

Java Spring启动-不重写服务器端口属性,java,spring,tomcat,properties,spring-boot,Java,Spring,Tomcat,Properties,Spring Boot,我有一个Spring引导项目,其中服务器端口总是设置为8080,而不管server.port属性如何。正确覆盖除server.port之外的所有属性。属性配置bean: @Bean public PropertySourcesPlaceholderConfigurer properties() { final PropertySourcesPlaceholderConfigurer poc = new PropertySourcesPlaceholderConfigurer();

我有一个Spring引导项目,其中服务器端口总是设置为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;
}
日志:


如果在项目中使用弹簧执行器,默认情况下,它指向 8080,如果您想更改它,那么在application.properties中
management.port=9001

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

您也可以通过编程自定义端口。请按照下面的链接进行操作。谢谢,我忘记了我在项目中添加了执行器,因为我还没有使用它。添加management.port已成功。您正在从eclipse运行项目吗?请注意,eclipse运行配置从类路径中排除application.properties文件(以及其他一些文件)。位于默认spring引导项目的resources文件夹中。请参阅project->properties->Java Build Path Source如果这种情况也发生在命令行中,那么只需在调试模式(--debug)下运行应用程序,您就会看到实际拾取了哪些属性文件您为什么要使用自己的配置?首先,它应该是静态的。但为什么不简单地将所有内容添加到
application.properties
或使用默认的spring引导机制呢?(已经支持您所做的)。使用而不是反对框架。@M.Deinum我删除了我的自制解决方案,使用了更简单的引导默认设置(application.properties位于/config子文件夹中)。起初它不起作用,这就是为什么我制定了自己的解决方案,但现在我让它起作用了。谢谢
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