Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Environment agnostic applications with Spring

Environment agnostic applications with Spring

Slides from Szczecin Java User Group meetup that took place on the 17th of December 2014.

Story about building multienvironment applications, starting from Maven profiles, through simple property placeholder based solutions, Spring @Profiles and in the end Spring @Conditionals and Spring Boot

Maciej Walkowiak

December 17, 2014
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. Environment
    agnostic applications
    with Spring
    Maciej Walkowiak
    !

    View Slide

  2. " " "
    DEV TEST PROD
    How to write application for multiple
    different environments?

    View Slide

  3. Maven profiles
    Approach #1:

    View Slide

  4. Maven profiles
    mvn package -P prod

    View Slide

  5. Maven profiles
    # #
    MySQL Oracle

    View Slide

  6. Maven profiles
    # #
    MySQL Oracle

    View Slide

  7. Maven profiles
    mvn package -P prod,oracle
    # #
    MySQL Oracle

    View Slide

  8. Maven profiles
    $
    new feature!
    with two variants

    View Slide

  9. Maven profiles
    $
    new feature!
    with two variants

    View Slide

  10. Maven profiles
    $
    new feature!
    with two variants

    View Slide

  11. Maven profiles
    mvn package -P prod,oracle,feature-impl1
    $
    new feature!
    with two variants

    View Slide

  12. Maven profiles
    mvn package -P prod,oracle,feature-impl1,
    jboss,sec,s3,dk,wtf
    %

    View Slide

  13. Maven profiles
    mvn package -P prod,oracle,feature-impl1,
    jboss,sec,s3,dk,wtf
    %
    … and we’re still building one single artifact

    View Slide

  14. Configure in runtime
    Approach #2:

    View Slide

  15. • Tell application how to behave when you run it, not
    when you build it.
    • No environment specific configuration in build time.

    Build as simple as:
    Configure in runtime
    mvn package

    View Slide

  16. • Each environment has it’s own properties file on
    classpath


    • Picked from environmental variable
    Simple solutions often
    work
    location="classpath:application-${env}.properties"/>
    -Denv=dev

    View Slide

  17. • Since Spring 3.1 (December 2011)
    • Profiles tag group of beans into sets




    • There can be multiple profiles enabled at once
    • Activated by environmental variable
    Spring @Profiles
    -Dspring.profiles.active=dev
    & & &
    “dev” “test” “prod”

    View Slide

  18. Spring @Profiles

    View Slide

  19. Spring @Profiles

    View Slide

  20. Spring @Profiles
    %

    View Slide

  21. Spring @Profiles
    -Dspring.profiles.active=prod,oracle,feature-
    impl1,sec,s3,dk,wtf
    %

    View Slide

  22. Spring @Profiles
    -Dspring.profiles.active=prod,oracle,feature-
    impl1,sec,s3,dk,wtf
    %
    … too many profiles can hurt

    View Slide

  23. Spring 4 @Conditionals
    • Conditionally creates bean
    • @ConditionalOnExpression!
    • @ConditionalOnProperty!
    • @ConditionalOnClass!
    • @ConditionalOnWebApplication!
    • …!
    • “@YourCustomConditional”!
    • @Profile is just a @Conditional specialisation
    Spring Boot

    View Slide

  24. Spring 4 @Conditionals
    # one of: filesystem,s3,mongo

    storage=filesystem

    View Slide

  25. Spring 4 @Conditionals
    # one of: filesystem,s3,mongo

    storage=filesystem
    @ConditionalOnProperty(name = "storage", havingValue = "s3")

    class S3StorageService implements StorageService {

    }


    @ConditionalOnProperty(name = "storage", havingValue = "mongo")

    class MongoStorageService implements StorageService {

    }


    @ConditionalOnProperty(name = "storage", havingValue = "filesystem",
    matchIfMissing = true)

    class FileSystemStorageService implements StorageService {

    }

    View Slide

  26. Spring Boot
    • Automatically picks up right
    application-${profile}.properties !
    • YAML format support
    • Properties loading hierarchy
    Spring Boot

    View Slide

  27. Demo
    Spring Boot
    @Value
    @PropertySource
    @Configuration
    Properties
    Logback

    View Slide

  28. Build once run everywhere

    View Slide

  29. • make build process as simple as possible
    • externalize application configuration
    • externalize logging configuration
    • provide defaults!
    • use Spring Boot
    Build once run everywhere

    View Slide

  30. Q & A

    View Slide

  31. Thank you!
    ' maciejwalkowiak.pl
    (@maciejwalkowiak

    View Slide