Master Spring Boot 3.2.5: Maven Property Expansion & Advanced Config
This guide demonstrates how to leverage Maven resource filtering for automatic property expansion in Spring Boot 3.2.5, configure externalized settings via spring.main and system properties, customize configuration file locations, activate profiles, set parent containers, modify DispatcherServlet paths, enable Tomcat access logs, MBean registry, and generate build and Git metadata.
1. Maven automatic property expansion
Using resource filtering you can expand Maven project properties with @...@ placeholders. Example pack.yml :
<code>pack:
app:
java-version: "java.version@"
springboot-version: "@project.parent.version@"
sourceEncoding: "@project.build.sourceEncoding@"
</code>Inject values:
<code>@Value("${pack.app.java-version}")
private String javaVersion;
@Value("${pack.app.springboot-version}")
private String springBootVersion;
@Value("${pack.app.sourceEncoding}")
private String sourceEncoding;
</code>Result:
<code>java-version: 17.0.9, springboot-version: 3.2.5, sourceEncoding: UTF-8</code>Maven parent definition:
2. SpringApplication externalized configuration
Set properties under spring.main.* to externalize configuration, e.g.:
<code>spring:
main:
web-application-type: servlet
banner-mode: off
</code>Other properties can be set programmatically:
<code>SpringApplication app = new SpringApplication(Application.class);
app.setWebApplicationType(WebApplicationType.SERVLET);
app.setBannerMode(Mode.CONSOLE);
app.run(args);
</code>File‑based configuration overrides code settings.
3. Changing configuration file location and name
Use system properties or environment variables:
spring.config.name (or SPRING_CONFIG_NAME ) – default application
spring.config.location (or SPRING_CONFIG_LOCATION ) – path or URL of the file
Example startup commands:
<code>java -Dspring.config.name=app -jar MyApp.jar
java -Dspring.config.location=d:\xxxooo\app.yml -jar MyApp.jar
</code>4. Activating profiles
Specify active profiles in application.yml :
<code>spring:
profiles:
active:
- dev
</code>If no profile is set, the default profile is used; you can change it with:
<code>spring:
profiles:
default: dev
</code>Profile activation image:
5. Setting parent and child containers
<code>SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
builder.child(ChildConfig.class);
builder.parent(ParentConfig.class);
builder.build().run(args);
</code>6. Customizing DispatcherServlet path
<code>spring:
mvc:
servlet:
path: /api
</code>Programmatic registration:
<code>@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration() {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet, "/api");
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(0);
return registration;
}
</code>7. Configuring access logs
<code>server:
tomcat:
basedir: "pack-tomcat"
accesslog:
enabled: true
pattern: "%t %a %r %s (%D microseconds)"
</code>Logs are written under the Tomcat base directory; you can change the directory with the basedir setting.
8. Enabling Tomcat MBean registry
<code>server:
tomcat:
mbeanregistry:
enabled: true
</code>After enabling, MBean metrics are visible via JConsole.
9. Generating build information
Add the Spring Boot Maven plugin with the build-info goal:
<code><plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</code>10. Generating Git information
Include the Git commit ID plugin:
<code><plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
</plugin>
</code>After building, a git.properties file is packaged in the JAR and can be inspected for repository state.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.