Skip to content

Spring Framework Blog

Menu
  • Home
  • Spring
  • Spring Boot
  • About
Menu

PropertySource and Profiles

Posted on November 22, 2023June 10, 2024 by Armando Marques

Table of Contents

Toggle
  • PropertySource
  • Profiles

PropertySource

Represents loaded properties from runtime environments.

A PropertySource is a simple abstraction over any source of key-value pairs, and Spring’s StandardEnvironment is configured with two PropertySource objects — one representing the set of JVM system properties (System.getProperties()) and one representing the set of system environment variables
(System.getenv()).

    Profiles

    Beans can be grouped in profiles, these can represent environment, implementation or deployment platforms.

    @Profile({exp,exp1,…})

    Create a new Profiles instance that checks for matches against the given profile expressions.
    The returned instance will match if any one of the given profile expressions matches.

    The following operators are supported in profile expressions.

    • ! – A logical NOT of the profile name or compound expression
    • & – A logical AND of the profile names or compound expressions
    • | – A logical OR of the profile names or compound expressions

    Configuration:

    @Configuration
    @Profile("embedded")
    public class DevConfig {...}

    or

    @Configuration
    public class Config {
    
     @Bean(name="datasource")
     @Profile("embedded")
     public Datasource getDataSourceDev(){
     ...
     }
    
     @Bean(name="datasource")
     @Profile("!embedded")
     public Datasource getDataSourceProd(){
     ...
     }
    
    }

    Activate profile:

    -Dspring.profiles.active=embedded,jpa

    System.setProperty(“spring.profiles.active”,”embedded,jpa”)

    Tests

    Tests profile can be annotated with: @ActiveProfiles(“dev”)

    https://www.baeldung.com/spring-profiles

    • Spring
    • Documentation
    • References
    • Toc
    • Books
    • Certification
    • AOP
    • Config
    • Java
    • Java core
    • JDBC
    • JPA
    • Rest
    • Security
    • Spring
    • Spring Boot
    • Spring Core
    • Spring Data
    • Spring MVC
    • Spring Rest
    • Spring Security
    • Tests
    • Transactions
    • Uncategorized

    Recent Posts

    • Spring Annotations
    • Java Tests
    • Java operators
    • Java versions
    • Java Oracle Licenses
    • Configuration properties
    • MockMvc
    • Spring Security III
    • MVC Controller Method Params
    • JPA Methods
    • Transaction propagation and isolation
    • Spring JDBC
    • Spring Boot Auto-Configuration
    • Spring Resource interface
    • JSR 330 Standard Annotations
    • Spring Aware Interfaces
    • Spring Transactions
    • Spring Boot Core
    • MVC Rest
    • Spring Boot JPA
    ©2025 Spring Framework Blog | Built using WordPress and Responsive Blogily theme by Superb