function
When the spring boot project is started, the basic information of the project will be printed: service name, project version in pom, and the time when the jar package is built. It is convenient to check whether the service is updated to the correct version, and also to provide interface access
Implementation steps (steps 2)
step 1 add pre execution code for service startup
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; /** * Execute the pre code of service startup before initializing the 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 configuration variable
project: version: @project.version@ builddate: @maven.build.timestamp@
Note that the variable to get maven uses @ @, and using @ @ in @ Value is invalid