Introducing Spring profiles

Spring profiles (https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-definition-profiles-java) let you change the way your application behaves based on environments. This is achieved using the @Profile annotations and profile-specific configuration files, which can be activated by specifying the spring.profiles.active property. Based on the profile that we set here, Spring will choose the appropriate application.properties|application.yml files and will include/exclude components that are included/excluded for the specific profile using the @Profile annotation in the Java source code.

For example, if we set spring.profiles.active=prod, all the Spring components that have @Profile("prod") will be instantiated and any component that has @Profile("!prod") will be excluded. Similarly, Spring will load and use the application-prod.yml or application-prod.properties file if it is available on the classpath.

JHipster configures a dev and prod profile by default, and includes the application-dev.yml and application-prod.yml files in the src/main/resources/config folder, along with the base application.yml file. JHipster goes a step further and provides a dev and a prod profile for the Gradle build as well (also available for Maven) so that we can build/run the application for a particular profile, which is very handy. Here are the profile and database configurations defined in the application-dev.yml file:

...

spring:
profiles:
active: dev
include:
- swagger
# Uncomment to activate TLS for the dev profile
#- tls

...
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:file:./build/h2db/db/store;DB_CLOSE_DELAY=-1
username: store
password:
hikari:
poolName: Hikari
auto-commit: fals
...

The following profiles are available in a JHipster application:

dev

Tuned for development and productivity, it enables Spring dev tools, in-memory databases, and so on

prod

Tuned for production, it focuses on performance and stability

swagger

Enables Swagger documentation for the API

no-liquibase

Disables Liquibase and is useful in production environments where you don't want Liquibase to run

tls

Runs the application in TLS mode for security

 

Now, let's look at how we can package our application with the production profile.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.124.244