Any non-trivial application runs at least in three
different environments.
a. Development
b. Testing
c. Production
Depend on the type of environment, we apply specific
configurations to the application. For example, enabling debug logging level in
testing and development and enabling only error logging level in production
etc.,
How to configure environment specific properties?
Create properties file by following below naming
convention.
application-{profile_name}.properties
Example
application-prod.properties : Specify all production
specific properties
application-test.properties : Specify all test
specific properties
application-staging.properties : Specify all staging
specific properties
How to specify the profile while starting the
application?
Launch the application by passing below VM argument.
-Dspring.profiles.active={profile_name}
Example
-Dspring.profiles.active=test
Above statement tells spring to use ‘test’ profile.
Let’s add/update below three files to the application.
application.properties
# Setting log level to DEBUG logging.level.org.springframework.web=DEBUG # Application launch at this port server.port=9090
application-prod.properties
# Setting log level to DEBUG logging.level.org.springframework.web=ERROR # Application launch at this port server.port=8080
application-test.properties
# Setting log level to DEBUG logging.level.org.springframework.web=INFO # Application launch at this port server.port=7070
Total project structure looks like below.
If you run App.java, it takes application.properties
by default.
Let’s run App.java.
You can see below messages in console.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE) 2019-07-06 23:13:11.985 INFO 557 --- [ main] com.sample.app.App : Starting App on C02X902SJGH5 with PID 557 (/Users/krishna/Documents/EclipseWorkSpaces/Learnings/springbootApp/target/classes started by krishna in /Users/krishna/Documents/EclipseWorkSpaces/Learnings/springbootApp) 2019-07-06 23:13:11.987 INFO 557 --- [ main] com.sample.app.App : No active profile set, falling back to default profiles: default 2019-07-06 23:13:12.883 INFO 557 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9090 (http) 2019-07-06 23:13:12.913 INFO 557 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2019-07-06 23:13:12.913 INFO 557 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21] 2019-07-06 23:13:13.013 INFO 557 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2019-07-06 23:13:13.013 DEBUG 557 --- [ main] o.s.web.context.ContextLoader : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT] 2019-07-06 23:13:13.014 INFO 557 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 987 ms 2019-07-06 23:13:13.127 DEBUG 557 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/**/favicon.ico] in 'faviconHandlerMapping' 2019-07-06 23:13:13.237 INFO 557 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2019-07-06 23:13:13.246 DEBUG 557 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice 2019-07-06 23:13:13.319 DEBUG 557 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 8 mappings in 'requestMappingHandlerMapping' 2019-07-06 23:13:13.328 DEBUG 557 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping' 2019-07-06 23:13:13.336 DEBUG 557 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice 2019-07-06 23:13:13.354 INFO 557 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [public/index.html] 2019-07-06 23:13:13.462 INFO 557 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9090 (http) with context path '' 2019-07-06 23:13:13.465 INFO 557 --- [ main] com.sample.app.App : Started App in 1.815 seconds (JVM running for 2.181)
As you closely observe the console messages, you can
see that tomcat started at port 9090 (‘Tomcat initialized with port(s): 9090’).
Open the url ‘http://localhost:9090/’ in browser, you
can see below screen.
How to run the application using prod profile?
Pass the below VM argument while launching the
application.
-Dspring.profiles.active=prod
Right click on App.java.
Run As -> Run Configurations…
Go to ‘Arguments’ tab and update below value under VM
arguments section.
-Dspring.profiles.active=prod
Click on Run button.
Open the url ‘http://localhost:8080/’ in browser, you
can see below kind of screen.
You can download complete working application from this link.
No comments:
Post a Comment