Slide 1

Slide 1 text

Environment agnostic applications with Spring Maciej Walkowiak !

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Maven profiles Approach #1:

Slide 4

Slide 4 text

Maven profiles mvn package -P prod

Slide 5

Slide 5 text

Maven profiles # # MySQL Oracle

Slide 6

Slide 6 text

Maven profiles # # MySQL Oracle

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Maven profiles $ new feature! with two variants

Slide 9

Slide 9 text

Maven profiles $ new feature! with two variants

Slide 10

Slide 10 text

Maven profiles $ new feature! with two variants

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Configure in runtime Approach #2:

Slide 15

Slide 15 text

• 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

Slide 16

Slide 16 text

• Each environment has it’s own properties file on classpath
 
 • Picked from environmental variable Simple solutions often work -Denv=dev

Slide 17

Slide 17 text

• 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”

Slide 18

Slide 18 text

Spring @Profiles

Slide 19

Slide 19 text

Spring @Profiles

Slide 20

Slide 20 text

Spring @Profiles %

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Spring 4 @Conditionals # one of: filesystem,s3,mongo
 storage=filesystem

Slide 25

Slide 25 text

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 {
 }

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Demo Spring Boot @Value @PropertySource @Configuration Properties Logback

Slide 28

Slide 28 text

Build once run everywhere

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Q & A

Slide 31

Slide 31 text

Thank you! ' maciejwalkowiak.pl (@maciejwalkowiak