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

2da55e1342d18933f0bcaf804a02f200?s=128

Maciej Walkowiak

December 17, 2014
Tweet

More Decks by Maciej Walkowiak

Other Decks in Programming

Transcript

  1. Environment agnostic applications with Spring Maciej Walkowiak !

  2. " " " DEV TEST PROD How to write application

    for multiple different environments?
  3. Maven profiles Approach #1:

  4. Maven profiles mvn package -P prod

  5. Maven profiles # # MySQL Oracle

  6. Maven profiles # # MySQL Oracle

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

  8. Maven profiles $ new feature! with two variants

  9. Maven profiles $ new feature! with two variants

  10. Maven profiles $ new feature! with two variants

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

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

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

    we’re still building one single artifact
  14. Configure in runtime Approach #2:

  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
  16. • Each environment has it’s own properties file on classpath


    
 • Picked from environmental variable Simple solutions often work <context:property-placeholder location="classpath:application-${env}.properties"/> -Denv=dev
  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”
  18. Spring @Profiles

  19. Spring @Profiles

  20. Spring @Profiles %

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

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

    hurt
  23. Spring 4 @Conditionals • Conditionally creates bean • @ConditionalOnExpression! •

    @ConditionalOnProperty! • @ConditionalOnClass! • @ConditionalOnWebApplication! • …! • “@YourCustomConditional”! • @Profile is just a @Conditional specialisation Spring Boot
  24. Spring 4 @Conditionals # one of: filesystem,s3,mongo
 storage=filesystem

  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 {
 }
  26. Spring Boot • Automatically picks up right application-${profile}.properties ! •

    YAML format support • Properties loading hierarchy Spring Boot
  27. Demo Spring Boot @Value @PropertySource @Configuration Properties Logback

  28. Build once run everywhere

  29. • make build process as simple as possible • externalize

    application configuration • externalize logging configuration • provide defaults! • use Spring Boot Build once run everywhere
  30. Q & A

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