---
title: "springboot获取pid和端口"
author: "tinyhema"
date: "2018-09-21T16:08:59Z"
tags: []
categories: ["移动开发"]
slug: "springboot-pid"
draft: false
lastmod: "2018-09-21T16:51:32Z"
id: 2431110
url: "https://tinyhema.iteye.com/blog/2431110"
---

自动生成 application.pid \ application.port 文件

1. 编码方式

```java

@SpringBootApplication
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(Application.class);
        app.addListeners(new ApplicationPidFileWriter()); 
        app.run(args);
    }

}


```


2. 配置文件方式
[参考官方文档](https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-process-monitoring.html)


```shell

# resources/META-INF/spring.factories 文件添加
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriter,\
org.springframework.boot.web.context.WebServerPortFileWriter

```




如果需要指定位置，可添加以下配置项

```shell

# application.properties
spring.pid.file=/var/log/app.pid

```