功能
在spring-boot项目启动时,将会打印项目的基本信息:服务名称,pom中的项目版本,以及jar包构建的时间,方便检测服务是否更新到正确的版本,也可以提供接口访问
实现步骤(steps 2)
step 1 添加服务启动的前置执行代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import javax.annotation.PostConstruct;
/**
* 初始化bean之前执行即服务启动的前置代码
*/
@Configuration
@Slf4j
public class BeanPostConfig implements BeanPostProcessor {
@Value("${project.version}")
private String serviceVersion;
@Value("${project.builddate}")
private String serviceBuildDate;
@Value("${spring.application.name}")
private String serviceName;
@Autowired
private Environment environment;
@PostConstruct
public void projectInfo(){
StringBuffer projectInfo = new StringBuffer();
projectInfo.append("\n=================project=================\n");
projectInfo.append(String.format("\nservice name:%s\n",serviceName));
projectInfo.append(String.format("\nservice version:%s\n",serviceVersion));
projectInfo.append(String.format("\nservice build date:%s\n",serviceBuildDate));
projectInfo.append("\n=================project=================\n");
log.info(projectInfo.toString());
}
}
step 2 application.yml配置变量
project:
version: @project.version@
builddate: @maven.build.timestamp@
注意获取maven的变量是使用@@,并且在@Value中使用@@是无效的,bootstrap.yml也无效,具体原理以后在了解一下
网友评论