Sunday, January 22, 2023

Spring Boot configuration through - application.properties

Spring boot comes with preconfigured application. We don't need to change anything to simply run the code. But if we want to change some of those default configurations to some customized configurations, we can use a file this method. For example if we want to connect to database and access the data, Spring Boot automatically provides configuration for almost anything, but, we also need to provide our database credentials, context path, etc. to run the program. So in that scenario, we need to provide those information to the application. There is a file named application.properties which can be used to store all the configuration properties in the spring boot application. These properties can include the port number on which we can run spring boot program, view settings, database and JPA settings and more. This file is in the folder /src/main/resources. There is also another file named application.yml which can be used to change the configuration. But the most common is using application.properties. There are different types of properties that can be changed through this file
    1. Core properties
      Cache properties
      Mail properties
      JSON properties
      Data properties
      Transaction properties
      Data migration properties
      Integration properties
      Web properties
      Templating properties
      Server properties
      Security properties
      RSocket properties
      Actuator properties
      Devtools properties
      Testing properties
  • We describe wome common properties we encounter while coding spring boot application.

    Changing port number on which we run spring boot application

    Spring boot comes with preconfigured port number on which the application is run. The default port number is 8080. But if the port is already being used or we want to use other port. e.g. 9000, then we can use this property to change it. server.port = 9000

    Change the view suffix and prefix

    The default folder containing view files is /src/webapps. If we want to put our view files in /src/webapps/views, then the first command does this. The second command tells spring to take .jsp as the defailt suffix or default extension that is used as view file spring.mvc.view.prefix=/views/ spring.mvc.view.suffix=.jsp

    Context path

    By default the context path is not available. If we want it, we can use following server.servlet.context-path=myapp

    Database url configuration

    We also need to use database confituration to use database with our spring application. Following properties are used to change the database credentials, and jpa properties. spring.datasource.url=jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update

    No comments:

    Post a Comment