UNIT - 2 - Server Programming Notes
UNIT - 2 - Server Programming Notes
REFERENCE MATERIAL
[Link]. (Computer
Science)
Server
Programming
Notes
(MR23-
1BSCC18)
*******************
Page |2
UNIT – I:
Spring Framework: Spring Framework Architecture, Modules, Spring
Dependencies, Spring Initializer, Spring vs Struts in Java, Spring MVC
UNIT– II:
Spring Boot: Micro services, Architecture, Auto configurations, Initialize, Install
Spring Tool Suite IDE, Creating an application, Dependency Management (Maven
and Gradle), Annotations, YAML Files, Actuators, Spring Boot Rest APIs creation.
UNIT – III:
Spring Boot: Micro services, Architecture, Auto configurations, Initialize, Install
Spring Tool Suite IDE, Creating an application, Dependency Management (Maven
and Gradle), Annotations, YAML Files, Actuators, Spring Boot Rest APIs creation.
UNIT – IV:
Spring boot JDBC: JDBC login application, Data JDBC, Data JPA, Spring Boot JPA.
Spring Boot: Thyme leaf Templates, Spring Boot Security.
UNIT – V:
SQLite Integration with Spring Boot: Introduction to SQLite – Features and
Advantages – Comparison with MySQL and MongoDB – SQLite Data Types –
Installation and Database Creation – Basic SQL Commands (CREATE, INSERT,
UPDATE, DELETE, SELECT) – Configuring SQLite in Spring Boot – Connecting with
Spring Data JPA – Performing CRUD Operations – Exception Handling – Creating
Executable JAR and Deployment.
~:0:~
Page |3
6. Production Readiness
Spring Framework: Needs additional tools.
Spring Boot: Provides Actuator for monitoring and health checks.
Comparison Table
Feature Spring Spring Boot
Framework
Type Core framework Framework
extension
Configuration Manual Auto-configured
Server External Embedded
Dependencie Individual Starters
s
Deployment WAR Executable JAR
Spring Boot simplifies the use of the Spring Framework by providing
auto-configuration, embedded servers, and starter dependencies,
making it ideal for modern, microservice-based applications.
2. What are Microservices, and how does Spring Boot support
microservices architecture?
Microservices are an architectural style where an application is
developed as a collection of small, independent, and loosely
coupled services, each responsible for a specific business function and
communicating through lightweight protocols (HTTP/REST).
What Are Microservices?
Each service performs one business capability.
Services are independently developed, deployed, and scaled.
Communication occurs via REST APIs or messaging.
Each service can use its own database.
Improves scalability, reliability, and maintainability.
How Spring Boot Supports Microservices
1. Standalone Service Development
Page |5
}
}
2. Controller Layer
Handles HTTP requests from clients.
Uses annotations like @RestController or @Controller.
Maps URLs to handler methods.
3. Service Layer
Contains business logic.
Acts as an interface between Controller and Repository.
Annotated with @Service.
4. Repository (DAO) Layer
Handles database operations.
Uses Spring Data JPA or JDBC.
Annotated with @Repository.
5. Embedded Web Server
Spring Boot includes embedded servers like Tomcat, Jetty, or
Undertow.
Eliminates need for external server deployment.
6. Auto-Configuration
Automatically configures beans based on dependencies.
Reduces manual configuration.
7. Dependency Management
Uses Spring Boot Starter dependencies.
Ensures compatible library versions.
8. Application Configuration
Uses [Link] or [Link].
Supports externalized configuration.
9. Spring Boot Actuator
Provides monitoring, health checks, and metrics.
Useful for production environments.
Advantages of Spring Boot Architecture
Loose coupling
High scalability
Page |8
o WebMvcAutoConfiguration
4. Conditional Annotations
Auto-configuration uses conditions such as:
@ConditionalOnClass – activates if a class exists
@ConditionalOnMissingBean – activates if bean not defined
@ConditionalOnProperty – activates based on property value
This ensures only required beans are created.
5. application. properties / [Link]
Developers override default behavior using configuration files.
Example:
[Link]=8081
[Link]=jdbc:mysql://localhost/db
6. Customization and Exclusion
Auto-configuration can be disabled or customized:
@SpringBootApplication(exclude = [Link])
Advantages of Auto-Configuration
Reduces boilerplate code
Faster development
Sensible defaults
Easy customization
Spring Boot auto-configuration works by detecting classpath
dependencies, applying conditional logic, and creating required
beans automatically, allowing developers to focus on business logic
instead of configuration.
5. How do you initialize a Spring Boot project using Spring Initializr?
Spring Initializr is a tool that helps developers quickly create
and configure a Spring Boot project with minimal manual effort.
Steps to Initialize a Spring Boot Project
1. Open Spring Initializr
Go to Spring Initializr website or use IDE support.
Common IDEs: STS, Eclipse, IntelliJ IDEA.
2. Select Project Details
Choose the following options:
P a g e | 10
Click Finish.
6. Configure Maven/Gradle
STS auto-configures Maven or Gradle.
Ensure dependencies are downloaded successfully.
Check [Link] for correctness.
7. Run the Spring Boot Application
Right-click project → Run As → Spring Boot App.
Embedded server starts automatically.
8. Optional Configuration
Use [Link] or [Link].
Enable Spring Boot Dashboard for monitoring apps.
Advantages of Using STS
Built-in Spring Boot support
Easy project creation
Integrated debugging and testing
Developer-friendly UI
Installing and configuring STS involves setting up JDK, installing
STS, creating a Spring Boot project, and running the application,
making it an efficient IDE for Spring Boot development.
7. How do you create and run a simple Spring Boot application?
A Spring Boot application can be created and run easily using
Spring Initializr and an IDE like Spring Tool Suite (STS) or IntelliJ
IDEA.
Steps to Create a Simple Spring Boot Application
1. Create Project Using Spring Initializr
Open Spring Initializr (web or IDE).
Select:
o Project: Maven
o Language: Java
o Packaging: JAR
o Java Version: 17
Add dependency: Spring Web.
Generate and open project in IDE.
P a g e | 13
@GetMapping("/hello")
public String hello() {
return "Hello Spring Boot";
}
}
Steps to Run the Application
4. Run from IDE
Right-click main class → Run As → Spring Boot App.
Embedded Tomcat server starts.
5. Run from Command Line (Optional)
mvn spring-boot:run
6. Access the Application
Open browser:
[Link]
Output:
Hello Spring Boot
Advantages of Spring Boot Application
Minimal configuration
P a g e | 14
Embedded server
Fast startup
Easy deployment
Creating and running a Spring Boot application involves project
creation, defining a controller, and running the application,
allowing developers to quickly build standalone web applications.
8. Explain dependency management in Spring Boot using Maven and
Gradle. How does Spring Boot simplify it?
Dependency management in Spring Boot refers to handling
external libraries required for application development. Spring Boot
simplifies dependency management by providing starter
dependencies and default version control using Maven and Gradle.
Dependency Management Using Maven
1. Maven in Spring Boot
Spring Boot uses Maven as a build and dependency management
tool.
Uses [Link] file.
2. Spring Boot Parent POM
Spring Boot provides a parent POM:
<parent>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.x.x</version>
</parent>
Manages dependency versions automatically.
3. Starter Dependencies
Starters are pre-configured dependency bundles.
Example:
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Includes:
P a g e | 15
Spring MVC
Embedded Tomcat
Jackson
Dependency Management Using Gradle
1. Gradle in Spring Boot
Uses [Link] or [Link].
Versions
Configuration Complex Simple
Setup Time High Low
Spring Boot simplifies dependency management in Maven and
Gradle by using starter dependencies, parent POM, and plugins,
allowing developers to focus on application logic instead of library
management.
9. What are commonly used Spring Boot annotations, and what is the
purpose of each?
Annotations in Spring Boot reduce configuration and simplify
application development by providing declarative programming
support.
1. Core Application Annotations
@SpringBootApplication
Main annotation to start a Spring Boot application.
Combines @Configuration, @EnableAutoConfiguration, and
@ComponentScan.
@Configuration
Indicates a class contains Spring bean definitions.
@ComponentScan
Scans packages to detect Spring components automatically.
2. Component Annotations
@Component
Marks a class as a Spring-managed bean.
@Service
Used in service layer.
Contains business logic.
@Repository
Used in data access layer.
Provides exception translation for database operations.
3. Dependency Injection Annotations
P a g e | 17
@Autowired
Automatically injects dependencies.
Supports constructor, setter, and field injection.
@Qualifier
Resolves ambiguity when multiple beans exist.
4. Web & REST Annotations
@Controller
Marks a class as a web controller.
Returns views.
@RestController
Combines @Controller and @ResponseBody.
Returns data (JSON/XML).
@RequestMapping
Maps HTTP requests to controller methods.
@GetMapping / @PostMapping
Shortcut annotations for GET and POST requests.
@RequestParam
Binds request parameters to method arguments.
@PathVariable
Extracts values from URI path.
5. Configuration & Property Annotations
@Value
Injects values from properties files.
@ConfigurationProperties
Binds multiple properties to a Java class.
6. Database & JPA Annotations
@Entity
Marks a class as a JPA entity.
@Id
Specifies primary key.
@GeneratedValue
Auto-generates primary key values.
P a g e | 18
Summary Table
Annotation Purpose
@SpringBootApplication Bootstraps app
@Component / @Service / Bean creation
@Repository
@Autowired Dependency
injection
@RestController REST APIs
@RequestMapping URL mapping
@Entity Database mapping
Spring Boot annotations simplify development by reducing
configuration, enabling dependency injection, and supporting
web and database operations, making applications clean and
maintainable.
10. What are YAML configuration files in Spring Boot, and how do they
differ from [Link]?
Spring Boot supports externalized configuration using both
[Link] and [Link] (YAML) files to define
application settings.
What Are YAML Configuration Files?
YAML stands for YAML Ain’t Markup Language.
Uses indentation and hierarchy to represent configuration.
In Spring Boot, configuration is stored in [Link].
Mainly used for structured and hierarchical data.
[Link]
Uses key–value pairs.
Simple and flat structure.
Easy for small configurations.
Example:
[Link]=8080
[Link]=jdbc:mysql://localhost/db
[Link]
Uses indentation instead of dots.
P a g e | 19
3. Application Management
Dynamic log level changes.
Shutdown and restart (if enabled).
View configuration details.
4. Integration with Monitoring Tools
Works with tools like Prometheus, Grafana, and ELK.
Useful for cloud and microservices environments.
Security of Actuator Endpoints
Sensitive endpoints can be restricted.
Controlled using:
[Link]=health,info
Advantages of Spring Boot Actuator
Real-time monitoring
Easy management
Minimal configuration
Production-ready support
Spring Boot Actuator provides built-in monitoring and
management endpoints that help developers observe application
health, performance, and behavior, making it essential for
production-grade Spring Boot applications.
12. How do you create REST APIs using Spring Boot? Explain with key
annotations involved.
REST APIs in Spring Boot are used to build stateless web services that
exchange data in formats like JSON or XML. Spring Boot simplifies REST API
creation using Spring MVC and annotations.
Steps to Create REST APIs in Spring Boot
1. Add Required Dependency
Include Spring Web starter.
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. Create REST Controller
Use @RestController to define REST endpoints.
P a g e | 22
……………………………………………………………………………………………………………