Build RESTful APIs with Spring MVC
  • Introduction
  • Overview
  • An introduction to REST
  • Prerequisites
  • Getting Started
    • Project skeleton
    • Configure Spring WebMVC
    • Configure Datasource
    • Configure JPA
    • Configure Spring Security
    • Configure Swagger
    • Maven profiles and Spring profiles
  • Getting started with Spring Boot
    • Project skeleton
    • Configure Datasource
    • Configure JPA
    • Configure Spring Security
    • Configure Swagger
    • Maven profiles and Spring profiles
  • Build REST API
  • Handle Exceptions
  • Test APIs
  • Visualize and document REST APIs
  • Secure APIs
  • Upgrade to Spring Boot 1.4
Powered by GitBook
On this page

Was this helpful?

  1. Getting started with Spring Boot

Maven profiles and Spring profiles

Similar with the former vanilla version, we can define some Maven profiles to inject configuration for different project stages, such as dev, staging, prod. etc.

Open the project pom.xml file.

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <spring.profiles.active>dev</spring.profiles.active>
        <log4j.level>DEBUG</log4j.level>
    </properties>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources-dev</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</profile>

The above Maven profile(dev) will be activated by default, and it will add /src/main/reources-dev as resource folder.

The application.yml under /src/main/reources-dev will be packaged into the final package(jar or war).

We can define some other profile based application.yml for different stages. Every application.yml will add different configuration, such as in development stage, use a H2 database for easy testing, and in production profile, the application.yml will use a pool DataSource for better performance at runtime.

The above approach combine Maven profiles and Spring profile to get clean and simple configuration for applications.

Alternatively, Spring Boot provides more powerful capability to switch profile via environment variables. You can package all profile based configuration in the same application package, and add a parameter to select a profile when the application is bootstrapping.

java -jar ./app.jar -Dspring.profiles.active=staging
PreviousConfigure SwaggerNextBuild REST API

Last updated 5 years ago

Was this helpful?