Lecture 34: Spring Boot Annotations & Build Systems

BMC201 - Web Technology

Dr. Prashant Kumar Nag

2026-04-21

Lecture 34

Spring Boot Annotations & Build Systems

Week 9 | Unit V: Spring Boot & REST Services
BMC201 - Web Technology
Dr. Prashant Kumar Nag, Assistant Professor

Learning Objectives


  • Understand key Spring Boot annotations and their purposes
  • Differentiate between Spring and Spring Boot annotations
  • Configure Spring Boot applications using annotations
  • Compare Maven and Gradle build systems
  • Create and manage dependencies in pom.xml and build.gradle
  • Use Spring Boot starters effectively

Prerequisites


Before this lecture, quickly revise:

  • Spring Boot Configuration & Setup from Lecture 33
  • Java annotations basics (@Override, @Deprecated)
  • Maven/Gradle dependency management fundamentals

Syllabus Mapping


  • Unit V topic focus: Spring Boot Annotations & Build Systems
  • CO alignment: implementation understanding + configuration reasoning
  • Assessment alignment: short definitions + long implementation/design questions

Agenda


  • 5-minute recap from previous lecture
  • Concept deep dive: Spring Boot Annotations & Build Systems
  • Code/configuration walkthrough and output analysis
  • Debug checklist and exam-oriented summary

Introduction


Spring Boot annotations simplify configuration, while modern build systems automate dependency management and project builds.

Think About It


How do annotations reduce the XML configuration burden in traditional Spring applications?

Core Spring Boot Annotations


Spring Boot provides specialized annotations that simplify configuration:

Annotation Purpose Equivalent To
@SpringBootApplication Main app config @Configuration + @EnableAutoConfiguration + @ComponentScan
@EnableAutoConfiguration Auto-configure beans Conditional bean creation
@ConfigurationProperties Bind properties Type-safe config
@ConditionalOnClass Load bean if class exists Conditional loading
@ConditionalOnMissingBean Load if bean absent Default bean creation

@SpringBootApplication Breakdown


@SpringBootApplication                           // #<1>
public class DemoApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

// Equivalent to combining three annotations:
@Configuration                                   // #<2>
@EnableAutoConfiguration                         // #<3>
@ComponentScan                                   // #<4>
public class DemoApplication { ... }
  1. Single annotation for Spring Boot apps
  2. Allows defining @Bean methods
  3. Automatically configures beans based on classpath
  4. Scans current package and subpackages for @Component

Component Stereotype Annotations


Spring and Spring Boot share these stereotype annotations:

Annotation Layer Purpose
@Component Generic Any Spring-managed component
@Service Business Business logic layer
@Repository Data Data access layer (with exception translation)
@Controller Web Web MVC controller
@RestController Web RESTful web service (@Controller + @ResponseBody)
@Service                                         // #<1>
public class OrderService { ... }

@RestController                                  // #<2>
@RequestMapping("/api")
public class OrderController { ... }
  1. Marks business logic component — auto-detected by component scan
  2. RESTful controller — returns JSON/XML directly

Configuration & Bean Annotations


@Configuration                                   // #<1>
public class AppConfig {

  @Bean                                          // #<2>
  public DataSource dataSource() {
    return new HikariDataSource();
  }

  @Bean
  @Profile("dev")                                // #<3>
  public MailSender devMailSender() {
    return new MockMailSender();
  }
}
  1. Marks class as source of bean definitions
  2. Method return value registered as Spring bean
  3. Bean only loaded when “dev” profile is active

Conditional Loading Annotations


@Configuration
public class DatabaseConfig {

  @Bean
  @ConditionalOnClass(name = "com.mysql.cj.jdbc.Driver") // #<1>
  public DataSource mysqlDataSource() { ... }

  @Bean
  @ConditionalOnMissingBean(DataSource.class)   // #<2>
  public DataSource defaultDataSource() { ... }

  @Bean
  @ConditionalOnProperty(name = "cache.enabled", havingValue = "true") // #<3>
  public CacheManager cacheManager() { ... }
}
  1. Bean created only if MySQL driver is in classpath
  2. Bean created only if no other DataSource bean exists (fallback)
  3. Bean created only if property is set to specific value

@ConfigurationProperties Example


@Configuration
@ConfigurationProperties(prefix = "app.email")   // #<1>
public class EmailProperties {
  private String host;                           // #<2>
  private int port;
  private String username;
  private String password;

  // getters and setters
}

// In application.properties:
app.email.host=smtp.gmail.com                    // #<3>
app.email.port=587
app.email.username=myapp@example.com
app.email.password=secret
  1. Binds all properties with prefix app.email to this class
  2. Properties automatically mapped to fields
  3. Type-safe configuration — validated at startup

Maven vs Gradle Overview


Aspect Maven Gradle
Configuration XML (pom.xml) Groovy/Kotlin DSL (build.gradle)
Build Speed Slower (no incremental builds) Faster (incremental builds)
Flexibility Convention-based Highly flexible
Dependency Management Transitive dependencies Advanced dependency resolution
Popularity More widespread Growing rapidly

Both are fully supported by Spring Boot. Maven is more common in enterprises, Gradle in Android/modern projects.

Maven Build Configuration


<parent>                                         <!-- #1 -->
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.1.0</version>
</parent>

<dependencies>                                   <!-- #2 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
</dependencies>

<build>                                          <!-- #3 -->
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
  1. Inherit Spring Boot parent for version management
  2. Add starter dependency (version inherited from parent)
  3. Maven plugin for packaging and running Spring Boot app

Gradle Build Configuration


plugins {                                        // #<1>
  id 'org.springframework.boot' version '3.1.0'
  id 'io.spring.dependency-management' version '1.1.0'
  id 'java'
}

dependencies {                                   // #<2>
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {                            // #<3>
  useJUnitPlatform()
}
  1. Apply Spring Boot and dependency management plugins
  2. Add dependencies using Gradle notation (shorter than Maven)
  3. Configure test task to use JUnit 5

Common Spring Boot Starters


Starters bundle related dependencies:

Starter What It Includes
spring-boot-starter-web Spring MVC, Tomcat, Jackson
spring-boot-starter-data-jpa Hibernate, JPA, JDBC
spring-boot-starter-security Spring Security
spring-boot-starter-test JUnit, Mockito, AssertJ, Hamcrest
spring-boot-starter-actuator Production monitoring endpoints
spring-boot-starter-validation Hibernate Validator (JSR-303)

Maven:

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

Gradle:

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

Dependency Management Benefits


Spring Boot parent manages dependency versions automatically:

  • No need to specify versions for managed dependencies
  • Ensures compatible versions across all dependencies
  • Reduces version conflicts and dependency hell
  • Easy to override specific versions if needed
<!-- No version needed — managed by parent -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Override version if needed -->
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>2.1.214</version>
</dependency>

Common Build Commands


Maven commands:

mvn clean install              # Build and install to local repo
mvn spring-boot:run            # Run Spring Boot app
mvn package                    # Create JAR file
mvn test                       # Run tests

Gradle commands:

./gradlew build                # Build project
./gradlew bootRun              # Run Spring Boot app
./gradlew test                 # Run tests
./gradlew clean                # Clean build directory

Common Mistakes to Avoid


  • Forgetting @SpringBootApplication on main class
  • Using @Component on config classes instead of @Configuration
  • Not understanding annotation hierarchy (@RestController includes @Controller)
  • Mixing Maven and Gradle files in the same project
  • Not excluding spring-boot-devtools in production builds
  • Hardcoding values instead of using @ConfigurationProperties

AKTU Exam-Oriented Checklist


Prepare these high-probability questions:

  1. Explain @SpringBootApplication and its components
  2. Differentiate @Component, @Service, @Repository, @Controller
  3. What is @ConfigurationProperties and when to use it?
  4. Compare Maven and Gradle with examples
  5. List five Spring Boot starters and their purposes
  6. Explain conditional annotations with code examples

Summary


You now understand:

  • Key Spring Boot annotations and their purposes
  • Component stereotypes and when to use each
  • Configuration and conditional loading annotations
  • Maven and Gradle build systems
  • Spring Boot starters for simplified dependency management
  • Common build commands and best practices

Memory Booster


Syllabus memory points for Spring Boot Annotations & Build Systems:

  • Core recall: @SpringBootApplication, component stereotypes, @ConfigurationProperties, Maven vs Gradle
  • Exam compare: Traditional Spring XML config vs Spring Boot annotation-based config
  • Practical anchor: Create Spring Boot project with custom configuration properties

Live Demo


Live implementation for Spring Boot Annotations & Build Systems:

📋 Open Demo: Lecture 34 - Spring Boot Annotations & Build Systems

Demo checklist: - identify key Spring Boot annotations in a sample project - compare Maven pom.xml and Gradle build.gradle structures - add a starter dependency and verify it works

Resources & References


Structured Debug Checklist


  1. verify @SpringBootApplication is present on main class
  2. check component annotations match their layer responsibilities
  3. inspect build file (pom.xml/build.gradle) for dependency conflicts
  4. validate starter dependencies are correctly specified
  5. ensure no missing getters/setters in @ConfigurationProperties classes

Exam Preparation Questions: Short


  • Define @SpringBootApplication and list its components.
  • Differentiate @Component, @Service, @Repository, and @Controller.
  • What is the purpose of @ConfigurationProperties?
  • Compare Maven and Gradle build systems.
  • List five Spring Boot starters with their purposes.

Exam Preparation Questions: Long


  • Explain Spring Boot Annotations with examples and use cases.
  • Compare Maven and Gradle with project structure examples.
  • Discuss conditional annotations and auto-configuration in Spring Boot.
  • Explain how Spring Boot starters simplify dependency management.

Practice Task


  • Implement: Create a Spring Boot application with custom configuration properties.
  • Use @ConfigurationProperties to bind database settings.
  • Add appropriate component annotations to service and repository classes.
  • Document one annotation mistake you encountered and the exact fix.

Checklist


Can you:

  • explain Spring Boot Annotations & Build Systems in your own words?
  • identify and use appropriate annotations for different layers?
  • create a pom.xml or build.gradle for a Spring Boot project?
  • troubleshoot annotation-related startup errors?

Next Lecture


  • Topic: Lecture 35 - Spring Boot Actuator, Runners & Logging
  • Preparation required: revise this lecture summary and practice configuring a Spring Boot project

Spring Boot Annotations & Build Systems

Next: Lecture 35 - Spring Boot Actuator, Runners & Logging