Prof. (Dr. ) Harish Kumar MittalProfessor · Researcher · Educator

Project Based Learning in Java

Spring Boot Setup and Application Configuration

Spring Boot simplifies the setup and startup of Spring applications. Its auto-configuration supplies configuration when conditions such as available libraries and existing beans match. The application.properties file stores settings, such as the server port. A profile groups settings for an environment, such as development.

Set up a Spring Boot application and explain auto-configuration, properties and profiles.

Chapter 3.1 · Lesson 3 CO5 aligned Unit 3 · Chapter 3.1

Course Outcome (CO) states what a learner should be able to do after the course; CO1 to CO5 label the five syllabus outcomes.

Learning outcome: Run the project and explain auto-configuration, package structure and external properties.

Start here: Locate the main class and properties file, understand the default configuration, then run the supplied project and verify its response.

Spring Framework and Spring Boot

Key concepts

Spring Boot
Tools and conventions that simplify starting and configuring a Spring application.
Dependency
Here, a library included in the project; distinguish this from an object injected into another object.
Starter
A convenient dependency bundle for a capability such as web request handling.
Auto-configuration
Conditional configuration selected from available libraries, properties and existing beans.
Profile
A named set of configuration choices for an environment such as development.

Begin with the main class, then find application.properties. The main class starts the application and establishes package-discovery conventions. The properties file supplies values. Boot combines these with dependencies and explicit configuration; it does not guess your business rules.

Example: Changing server.port from 8080 to 8081 changes where the web application listens. It does not change its endpoint paths. An in-memory H2 database is temporary, so restarting the process removes its data in this exercise.

Model-View-Controller (MVC) separates application data and rules, user-facing presentation, and request or input handling.

Spring Framework supplies the container, dependency injection, transactions and MVC support. Spring Boot helps start and configure an application that uses those features.

A starter is a convenient set of related dependencies. Auto-configuration supplies configuration when its conditions match. Boot builds on Spring; you still need to understand the beans and settings your application uses.

The student-management example uses Java 17 and Maven. It pins Spring Boot 3.5.7 as a teaching baseline, not as a latest-release claim. Imports use jakarta.persistence and jakarta.annotation. Boot's dependency management selects compatible Hibernate and Spring libraries; do not mix unrelated versions.

Project layout

student-management/
  pom.xml
  src/main/java/edu/pblj/unit3/
    StudentApplication.java
    Student.java, Department.java, Course.java
    StudentRepository.java, StudentService.java
    StudentController.java, GlobalErrors.java
  src/main/resources/application.properties
  src/test/java/edu/pblj/unit3/StudentApplicationTests.java

Keep the main application class in a parent package of components, repositories and entities. This small example keeps them together; a larger application can use subpackages for web, service and persistence responsibilities.

package edu.pblj.unit3;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StudentApplication {
    public static void main(String[] args) {
        SpringApplication.run(StudentApplication.class, args);
    }
}

@SpringBootApplication combines Boot configuration, auto-configuration and component scanning. The application package also establishes conventions used for entity and repository discovery. A bean hidden in an unrelated package is not necessarily discovered.

Auto-configuration is conditional

Application Programming Interface (API) defines the operations one software component can use to communicate with another. Java Persistence API (JPA) lets Java code save and load objects as database rows. The standard is now called Jakarta Persistence. H2 Database Engine (H2) is a Java relational database often used in memory for isolated examples; H2 is a product name, not an acronym.

In this project, the web starter supplies the servlet-based web stack, the JPA starter brings Spring Data JPA and Hibernate, and H2 provides the local database.

Boot checks what is available before applying configuration: which dependencies are present, which properties are set and which beans already exist. For example, a custom DataSource bean can replace Boot's default database setup. This is called backing off. Start with --debug to see the conditions report when you need to understand a configuration decision.

Properties

Save the following as src/main/resources/application.properties. Each line sets one configuration key:

spring.application.name=pblj-student-management
server.port=8080
spring.datasource.url=jdbc:h2:mem:pblj
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.open-in-view=false
spring.jpa.show-sql=true
course.label=PBLJ Chapter 3.1

Read the settings in three groups:

Hypertext Transfer Protocol (HTTP) defines how a client sends a request and a server returns a response on the web. Structured Query Language (SQL) is the language used to define, query and change data in relational databases.

  • Application: spring.application.name names the application; server.port selects its HTTP port.
  • Database: the H2 database is in memory. It starts empty and its data disappears when the process ends. create-drop creates disposable tables for this exercise; use migrations to manage a persistent schema.
  • Data access: open-in-view=false disables keeping a JPA persistence context open for the whole web request. Fetch the data needed for the response inside the service transaction. SQL logging helps you see database work, but deployed logs should not expose sensitive data.

course.label is an application-specific setting read by CourseBanner in Lesson 2.

Custom properties can be injected with @Value; grouped properties can use @ConfigurationProperties. For example, CourseBanner reads course.label from the properties file. The injected value configures that bean; it does not change which Java class it is.

A profile selects configuration for an environment. For development, put overrides in application-dev.properties and activate them with --spring.profiles.active=dev.

You can also override packaged settings without editing that file. For example, the environment variable SERVER_PORT=8081 sets server.port. Among these common sources, command-line arguments override environment variables, and environment variables override packaged configuration files. Other sources also exist; the reference below gives the complete order. Keep real credentials out of source files and downloads.

Worked example: choosing a port

Suppose the application needs port 8081 for one run. Leave server.port=8080 in the packaged file and supply a command-line override:

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081

Open http://localhost:8081/students. When the argument is omitted on the next run, the packaged default applies again, provided no higher-priority source overrides it. Changing the database URL or an active profile can change which data the application sees; check the active configuration before running schema-changing operations.

PropertyPurpose in this exampleWhat to check
server.portSelects the HTTP listening port.Open the application at that port.
spring.datasource.urlSelects the database connection.Check the URL and database lifecycle.
spring.jpa.hibernate.ddl-autoControls Hibernate's schema action.Use create-drop only with disposable data.
spring.jpa.open-in-viewControls persistence-context availability during web rendering.Fetch response data within the service transaction.

Run and verify

Follow these steps in order. The first Maven dependency build needs internet access:

Java Development Kit (JDK) provides the compiler, runtime and other tools needed to build and run Java programs. JavaScript Object Notation (JSON) is a text format for exchanging values such as objects, arrays, strings, numbers and booleans.

  1. Download the student-management example linked here.
  2. Extract the ZIP. Open a terminal in the project folder containing pom.xml.
  3. Run java -version and mvn -version. Use JDK 17 for this teaching baseline, with Maven 3.6.3 or later. Check that Maven uses this JDK.
  4. Run mvn test and wait for the tests to pass.
  5. Run mvn spring-boot:run and wait for the application to start.
  6. Open http://localhost:8080/students in a browser. A fresh database should return [], meaning an empty JSON list.
  7. Return to the terminal and press Ctrl+C to stop the application.

Run the test command before starting the application:

mvn test
mvn spring-boot:run

If port 8080 is busy, change server.port or run mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081. Then use http://localhost:8081/students in the browser. The in-memory data is lost when the application stops.

Representational State Transfer (REST) is an architectural style that organises a web service around resources, such as students or products, accessed through a consistent interface.

The controller is a small harness for persistence and exception-handling exercises. Chapter 3.2 develops REST design. Confirm java -version and mvn -version use the intended JDK if compilation fails.

Practice

Uniform Resource Locator (URL) is a resource identifier that describes where a resource can be accessed and the scheme used to access it.

  1. Change course.label and identify the changed startup output.
  2. Run on port 8081 and verify the URL.
  3. Explain why students disappear after a restart.

Quick check

Check your understanding

Does auto-configuration always create the same beans? No. It depends on dependencies, settings and existing configuration.

Further reading

Review questions

1. Does adding a web starter create your StudentService business rules?

Answer: No. The starter supplies infrastructure dependencies. You still write and register application components.

2. Why can a custom DataSource change Boot’s database setup?

Answer: Auto-configuration checks for existing beans. A matching explicit bean can cause the default configuration to back off instead of creating a competing bean.