Saturday 3 August 2019

Spring boot: application.properties


This is continuation to my previous post, you can download previous application from below github link.

Spring boot comes with default configurations.

For example, when I add ‘spring-boot-starter-web’ dependency, spring boot automatically embed tomcat container, uses default message converters (like convert json to java POJO and POJO to json string) etc.,

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Sometimes we may want to customize these default configurations. You can customize these default configurations using ‘application.properties’ file.

Where to put application.properties file?
Place this file in application root classpath (You can put this file in some other places also, for simplicity sake, I am putting this file in application root classpath).

In case of maven project, we should place this file in ‘src/main/resources’ folder.


To demonstrate the application, I set the log level to DEBUG (Default level is INFO) and server port to 9090 (Default port is 8080).

application.properties

# Setting log level to DEBUG
logging.level.org.springframework.web=DEBUG

# Setting server port to 9090
server.port=9090


Run App.java, you can see below messages in console.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-07-06 15:16:59.704  INFO 99778 --- [           main] com.sample.app.App                       : Starting App on C02X902SJGH5 with PID 99778 (/Users/krishna/Documents/EclipseWorkSpaces/Learnings/springbootApp/target/classes started by krishna in /Users/krishna/Documents/EclipseWorkSpaces/Learnings/springbootApp)
2019-07-06 15:16:59.706  INFO 99778 --- [           main] com.sample.app.App                       : No active profile set, falling back to default profiles: default
2019-07-06 15:17:00.722  INFO 99778 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2019-07-06 15:17:00.754  INFO 99778 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-07-06 15:17:00.754  INFO 99778 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-06 15:17:00.860  INFO 99778 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-07-06 15:17:00.860 DEBUG 99778 --- [           main] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2019-07-06 15:17:00.860  INFO 99778 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1106 ms
2019-07-06 15:17:00.978 DEBUG 99778 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/**/favicon.ico] in 'faviconHandlerMapping'
2019-07-06 15:17:01.084  INFO 99778 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-06 15:17:01.093 DEBUG 99778 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2019-07-06 15:17:01.169 DEBUG 99778 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : 8 mappings in 'requestMappingHandlerMapping'
2019-07-06 15:17:01.180 DEBUG 99778 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2019-07-06 15:17:01.187 DEBUG 99778 --- [           main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2019-07-06 15:17:01.207  INFO 99778 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [public/index.html]
2019-07-06 15:17:01.312  INFO 99778 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path ''
2019-07-06 15:17:01.315  INFO 99778 --- [           main] com.sample.app.App                       : Started App in 2.077 seconds (JVM running for 2.483)

As you see above messages, you can observe both DEBUG and INFO messages are printed to console.


Open the url ‘http://localhost:9090/’ in browser, you can see below kind of screen.


You can get complete working application from this link.

Change the port back to standard 8080 port or remove below ‘server.port’ property from application.properties file.

Previous                                                    Next                                                    Home

No comments:

Post a Comment