[Go to site: main page, start]

0% found this document useful (0 votes)
7 views18 pages

Java Backend Developer Study Guide-2

The document is a comprehensive guide for Java Backend Developers covering essential topics such as Core Java, Build Tools, Spring Ecosystem, Databases, API Design, and more. It includes definitions, examples, code snippets, and best practices related to object-oriented programming, Spring Boot, RESTful API principles, and database management. The guide serves as a reference for building scalable and maintainable backend applications using Java technologies.

Uploaded by

xepif75442
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Java Backend Developer Study Guide-2

The document is a comprehensive guide for Java Backend Developers covering essential topics such as Core Java, Build Tools, Spring Ecosystem, Databases, API Design, and more. It includes definitions, examples, code snippets, and best practices related to object-oriented programming, Spring Boot, RESTful API principles, and database management. The guide serves as a reference for building scalable and maintainable backend applications using Java technologies.

Uploaded by

xepif75442
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

☕ Java Backend Developer

Complete Study Reference Guide


Definitions · Examples · Code Snippets

Table of Contents
• 1. Core Java
• 2. Build Tools
• 3. Spring Ecosystem
• 4. Databases
• 5. API Design
• 6. Messaging & Events
• 7. Caching
• 8. Testing
• 9. DevOps & Infrastructure
• 10. Version Control
• 11. System Design
• 12. Logging & Monitoring
• 13. Security

01 Core Java

1.1 Object-Oriented Programming (OOP)


OOP is a paradigm based on organizing code into objects that bundle data and behavior together.

Class & Object


A Class is a blueprint; an Object is an instance of that blueprint.
📌 Example: A Car class defines properties like speed, color. A Toyota Corolla is an object of Car.
class Car {
String brand;
int speed;
void drive() { [Link]("Driving " + brand); }
}
Car myCar = new Car();
[Link] = "Toyota";

Inheritance
A child class inherits fields and methods from a parent class using the extends keyword.
📌 Example: A Dog class inherits from Animal, reusing eat() method but adding bark().
class Animal { void eat() { [Link]("Eating"); } }
class Dog extends Animal { void bark() { [Link]("Bark!"); } }

Polymorphism
The ability for one interface to be used with different underlying types — same method name,
different behavior.
📌 Example: [Link]() behaves differently for Circle vs Rectangle.
class Shape { void draw() {} }
class Circle extends Shape { void draw() { [Link]("Drawing Circle"); } }
Shape s = new Circle();
[Link](); // prints "Drawing Circle"

Encapsulation
Hiding internal data of a class and exposing it only through public methods (getters/setters).
📌 Example: A BankAccount hides balance and only allows deposit/withdraw methods.
class BankAccount {
private double balance;
public double getBalance() { return balance; }
public void deposit(double amount) { balance += amount; }
}

Abstraction
Hiding implementation details and exposing only relevant behavior via abstract classes or
interfaces.
📌 Example: A payment interface has pay() — whether it's PayPal or Card is hidden from the caller.
interface Payment { void pay(double amount); }
class CreditCard implements Payment {
public void pay(double amount) { [Link]("Paid " + amount + " via Card");
}
}

1.2 SOLID Principles


Five design principles for writing maintainable, scalable object-oriented code.
Principle Meaning Example
S - Single A class should have only one UserService handles only user logic, not email sending
Responsibili reason to change
ty

O- Open for extension, closed for Add new discount types via new classes, not editing
Open/Close modification existing ones
d

L - Liskov Subclasses must be Dog and Cat both extend Animal — either works
Substitution replaceable for parent types wherever Animal is expected

I - Interface Don't force classes to Split fat interface into Printable, Saveable, Shareable
Segregation implement unused methods
D- Depend on abstractions, not Inject PaymentGateway interface, not Stripe class
Dependency concrete classes directly
Inversion

1.3 Java 8+ Features

Lambda Expressions
Anonymous functions that let you write more concise code for functional interfaces.
📌 Example: Sort a list without an anonymous Comparator class.
// Before Java 8
[Link](names, new Comparator<String>() {
public int compare(String a, String b) { return [Link](b); }
});
// Java 8 Lambda
[Link]((a, b) -> [Link](b));

Stream API
Functional-style operations on collections — filter, map, reduce, collect without loops.
📌 Example: Get all names starting with "A" in uppercase.
List<String> result = [Link]()
.filter(n -> [Link]("A"))
.map(String::toUpperCase)
.collect([Link]());

Optional
A container that may or may not hold a value — eliminates NullPointerException.
📌 Example: Safely get a user's email without null checks.
Optional<User> user = [Link](id);
String email = [Link](User::getEmail).orElse("no-email@[Link]");

1.4 Collections Framework


Core data structures every Java developer must know:
Type Implementation Use When Key Trait
List ArrayList, LinkedList Ordered, allow duplicates Index-based access

Set HashSet, TreeSet Unique elements needed No duplicates allowed

Map HashMap, Key-value pairs Fast key lookup O(1)


LinkedHashMap

Queue PriorityQueue, FIFO processing poll() / peek()


ArrayDeque

1.5 Concurrency & Multithreading

ExecutorService & ThreadPool


Manages a pool of threads to execute tasks without creating a new thread per task.
📌 Example: Process 1000 orders concurrently using a thread pool of 10.
ExecutorService executor = [Link](10);
for (Order order : orders) {
[Link](() -> processOrder(order));
}
[Link]();

CompletableFuture
Enables non-blocking async operations with chaining.
📌 Example: Fetch user and their orders concurrently and combine results.
CompletableFuture<User> userFuture = [Link](() -> getUser(id));
CompletableFuture<List<Order>> ordersFuture = [Link](() ->
getOrders(id));
[Link](userFuture, ordersFuture).thenRun(() -> {
User u = [Link]();
List<Order> o = [Link]();
});

synchronized & volatile


synchronized prevents concurrent access to a block; volatile ensures visibility of variable
changes across threads.
📌 Example: A counter incremented by multiple threads.
class Counter {
private volatile int count = 0;
public synchronized void increment() { count++; }
public int getCount() { return count; }
}

02 Build Tools

Maven
Project management tool using [Link] to manage dependencies, build lifecycle, and plugins.
📌 Example: Add Spring Boot to your project.
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.2.0</version>
</dependency>

Maven Lifecycle
Ordered phases: validate → compile → test → package → verify → install → deploy.
📌 Example: Run mvn package to compile, test, and create a JAR.
mvn clean install # clean + full build cycle
mvn test # run tests only
mvn package # create .jar or .war
Gradle
An alternative build tool using Groovy/Kotlin DSL — faster with incremental builds.
📌 Example: Add a dependency in [Link].
// [Link]
dependencies {
implementation '[Link]:spring-boot-starter-web:3.2.0'
testImplementation '[Link]:junit-jupiter:5.10.0'
}

03 Spring Ecosystem

3.1 Spring Core

IoC Container & Dependency Injection


Inversion of Control means Spring creates and manages objects (beans). Dependency Injection
means Spring injects dependencies automatically.
📌 Example: UserService needs UserRepository — Spring injects it.
@Service
public class UserService {
private final UserRepository userRepo;
// Constructor Injection (preferred)
public UserService(UserRepository userRepo) {
[Link] = userRepo;
}
}

Bean & Annotations


A Bean is an object managed by Spring. @Component, @Service, @Repository are stereotypes
that register beans.
📌 Example: @Service marks UserService as a Spring bean for DI.
@Component // Generic bean
@Service // Business logic layer
@Repository // Data access layer
@Controller // Web layer
// All are picked up by @ComponentScan

3.2 Spring Boot

Auto-configuration
Spring Boot automatically configures your app based on jars on the classpath — no XML
needed.
📌 Example: Add spring-boot-starter-data-jpa and Spring auto-configures Hibernate, DataSource,
etc.
# [Link]
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: user
password: secret
jpa:
hibernate:
ddl-auto: validate

Spring Boot Actuator


Provides production-ready endpoints for health, metrics, info, and more.
📌 Example: Check if your app is up via /actuator/health.
# [Link]
management:
endpoints:
web:
exposure:
include: health,info,metrics
# GET /actuator/health → {"status":"UP"}

3.3 Spring MVC & REST

@RestController
Combines @Controller + @ResponseBody — all methods return JSON/XML directly.
📌 Example: A simple REST endpoint that returns a user.
@RestController
@RequestMapping("/api/users")
public class UserController {
@GetMapping("/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
return [Link]([Link](id));
}
@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody UserDto dto) {
return [Link](201).body([Link](dto));
}
}

@ControllerAdvice (Global Exception Handling)


Centralized exception handling across all controllers.
📌 Example: Return a proper 404 JSON response when a user is not found.
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler([Link])
public ResponseEntity<ErrorResponse> handleNotFound(ResourceNotFoundException ex) {
return [Link](404).body(new ErrorResponse([Link]()));
}
}

3.4 Spring Data JPA

Repository Pattern
Spring Data JPA generates query implementations automatically from method names.
📌 Example: Find users by email without writing SQL.
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
List<User> findByAgeGreaterThan(int age);
@Query("SELECT u FROM User u WHERE [Link] = :city")
List<User> findByCity(@Param("city") String city);
}

Entity & Relationships


Map Java classes to DB tables using JPA annotations.
📌 Example: A User has many Orders.
@Entity
public class User {
@Id @GeneratedValue(strategy = [Link])
private Long id;
@OneToMany(mappedBy = "user", cascade = [Link], fetch = [Link])
private List<Order> orders;
}

N+1 Problem
When fetching a list of entities triggers one extra query per entity to fetch its relationship. Use
JOIN FETCH to fix.
📌 Example: Fetching 100 users and their orders triggers 101 queries without JOIN FETCH.
// BAD - causes N+1
List<User> users = [Link]();
[Link](u -> [Link]()); // 100 extra queries

// GOOD - use JOIN FETCH


@Query("SELECT u FROM User u JOIN FETCH [Link]")
List<User> findAllWithOrders();

3.5 Spring Security

JWT Authentication
JSON Web Tokens allow stateless authentication — the server validates a signed token instead
of storing sessions.
📌 Example: User logs in, gets a JWT, sends it in every request header.
// Filter checks JWT on every request
@Override
protected void doFilterInternal(HttpServletRequest req, ...) {
String token = [Link]("Authorization").substring(7);
if ([Link](token)) {
// Set authentication in SecurityContext
}
}

Role-Based Access Control (RBAC)


Restrict endpoint access based on user roles (ADMIN, USER, etc.).
📌 Example: Only ADMIN can access /api/admin/** endpoints.
@Configuration
public class SecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
[Link](auth -> auth
.requestMatchers("/api/admin/**").hasRole("ADMIN")
.requestMatchers("/api/user/**").hasRole("USER")
.anyRequest().authenticated());
return [Link]();
}
}

04 Databases

4.1 SQL Fundamentals

Joins
Combine rows from multiple tables based on a related column.
📌 Example: Get user names with their order totals.
SELECT [Link], SUM([Link]) as total_spent
FROM users u
INNER JOIN orders o ON [Link] = o.user_id
GROUP BY [Link]
HAVING SUM([Link]) > 1000
ORDER BY total_spent DESC;

Indexes
A data structure that speeds up query lookups by maintaining a sorted copy of a column.
📌 Example: Add an index on email since users are frequently searched by email.
-- Create index
CREATE INDEX idx_users_email ON users(email);
-- Composite index
CREATE INDEX idx_orders_user_status ON orders(user_id, status);
-- Check if index is used
EXPLAIN SELECT * FROM users WHERE email = 'test@[Link]';

Transactions & ACID


Transactions group operations so they either all succeed or all fail. ACID: Atomicity,
Consistency, Isolation, Durability.
📌 Example: Transfer money between accounts atomically.
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;
-- If error occurs, ROLLBACK reverts both updates

4.2 NoSQL — MongoDB

Documents & Collections


MongoDB stores data as JSON-like documents in collections (similar to rows in tables).
Schema-flexible.
📌 Example: Store a product with varying attributes without fixed schema.
// Insert document
[Link]({
name: "Laptop",
price: 1200,
specs: { ram: "16GB", storage: "512GB SSD" },
tags: ["electronics", "computers"]
});
// Query
[Link]({ price: { $lt: 1500 } });

4.3 Redis

Key-Value Cache
Redis stores data in-memory for extremely fast reads. Used for caching, sessions, rate limiting.
📌 Example: Cache a user profile for 10 minutes.
// In Spring Boot
@Cacheable(value = "users", key = "#id")
public User getUserById(Long id) {
return [Link](id).orElseThrow();
}
// Redis CLI
SET user:42 '{"name":"Alice"}' EX 600
GET user:42

4.4 Database Migrations

Flyway
Version-control your database schema changes with SQL migration files. Runs automatically on
startup.
📌 Example: Add a phone column to users table.
-- File: V2__add_phone_to_users.sql
ALTER TABLE users ADD COLUMN phone VARCHAR(20);

-- Flyway tracks this in flyway_schema_history table


-- Naming: V{version}__{description}.sql

05 API Design

RESTful Principles
REST uses HTTP methods and resource-based URLs. Stateless: each request contains all
needed information.
📌 Example: Design a Users API.
GET /api/v1/users → List all users
GET /api/v1/users/{id} → Get user by ID
POST /api/v1/users → Create user
PUT /api/v1/users/{id} → Update entire user
PATCH /api/v1/users/{id} → Partial update
DELETE /api/v1/users/{id} → Delete user

HTTP Status Codes


Standard codes that tell clients what happened with the request.
📌 Example: 201 when user is created, 404 when not found, 400 for bad input.
200 OK → Success
201 Created → Resource created
204 No Content → Deleted successfully
400 Bad Request → Invalid input
401 Unauthorized → Not authenticated
403 Forbidden → No permission
404 Not Found → Resource missing
500 Internal Error → Server failure

API Versioning
Version your API to avoid breaking clients when making changes.
📌 Example: Keep v1 working while releasing v2.
// URL versioning (most common)
GET /api/v1/users
GET /api/v2/users

// Header versioning
GET /api/users
Headers: Accept: application/[Link].v2+json

Swagger / OpenAPI
Auto-generates interactive API documentation from your code annotations.
📌 Example: Add Swagger to your Spring Boot project.
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
// Access docs at: [Link]

06 Messaging & Event-Driven Architecture

Event-driven architecture decouples services by having them communicate through messages/events


rather than direct calls.

Apache Kafka
A distributed streaming platform for high-throughput, durable message passing. Topics →
Partitions → Consumer Groups.
📌 Example: Order service publishes "OrderPlaced" event; Inventory and Email services consume it
independently.
// Producer
@Autowired KafkaTemplate<String, String> kafkaTemplate;
public void publishOrder(Order order) {
[Link]("orders-topic", [Link]().toString(), toJson(order));
}

// Consumer
@KafkaListener(topics = "orders-topic", groupId = "inventory-group")
public void handleOrder(String orderJson) {
Order order = fromJson(orderJson);
[Link](order);
}

RabbitMQ
Message broker using exchanges and queues. Good for task queues and routing. Messages are
acknowledged and removed when processed.
📌 Example: Send an email asynchronously after user registration.
// Publish
[Link]("email-exchange", "[Link]", emailDto);

// Consume
@RabbitListener(queues = "email-queue")
public void sendEmail(EmailDto dto) {
[Link](dto);
}

Kafka vs RabbitMQ
Kafka: High throughput, durable, replay events, event sourcing, stream processing. Best for: audit
logs, analytics, microservice events.
RabbitMQ: Complex routing, task queues, request/reply patterns. Best for: job queues, notifications,
work distribution.

07 Caching

Cache-Aside Pattern
Application checks cache first, fetches from DB if miss, then stores in cache for next time.
📌 Example: Product catalog rarely changes — cache it for 1 hour.
@Cacheable(value = "products", key = "#id")
public Product getProduct(Long id) {
return [Link](id).orElseThrow();
}

@CacheEvict(value = "products", key = "#[Link]")


public Product updateProduct(Product product) {
return [Link](product);
}
TTL & Eviction Policies
TTL (Time-To-Live) auto-expires cache entries. Eviction policies decide what to remove when
cache is full.
📌 Example: Cache user sessions for 30 minutes; use LRU eviction.
# [Link] - Redis cache TTL
spring:
cache:
redis:
time-to-live: 1800000 # 30 minutes in ms

# Redis eviction policy ([Link])


maxmemory-policy allkeys-lru # Remove least recently used

08 Testing

8.1 Unit Testing — JUnit 5 & Mockito

JUnit 5 Basics
Framework for writing and running unit tests. @Test marks a test method; assertions verify
behavior.
📌 Example: Test that a discount is calculated correctly.
@Test
@DisplayName("Should apply 10% discount for premium users")
void shouldApply10PercentDiscount() {
// Arrange
User user = new User("Alice", [Link]);
// Act
double discount = [Link](user, 100.0);
// Assert
assertEquals(10.0, discount);
assertTrue(discount > 0);
}

Mockito
Creates mock objects so you can test a class in isolation by simulating its dependencies.
📌 Example: Test UserService without hitting the real database.
@ExtendWith([Link])
class UserServiceTest {
@Mock UserRepository userRepo;
@InjectMocks UserService userService;

@Test
void shouldReturnUserById() {
User mockUser = new User(1L, "Alice");
when([Link](1L)).thenReturn([Link](mockUser));

User result = [Link](1L);


assertEquals("Alice", [Link]());
verify(userRepo, times(1)).findById(1L);
}
}
8.2 Integration Testing

@SpringBootTest & MockMvc


Loads the full Spring context for integration testing. MockMvc lets you test HTTP endpoints
without starting a real server.
📌 Example: Test that POST /api/users returns 201 with correct body.
@SpringBootTest
@AutoConfigureMockMvc
class UserControllerTest {
@Autowired MockMvc mockMvc;

@Test
void shouldCreateUser() throws Exception {
String json = "{\"name\":\"Alice\",\"email\":\"a@[Link]\"}";
[Link](post("/api/users")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.name").value("Alice"));
}
}

Testcontainers
Spins up real Docker containers (PostgreSQL, Redis, Kafka) during tests for realistic integration
testing.
📌 Example: Test your repo against a real PostgreSQL DB in Docker.
@Testcontainers
@SpringBootTest
class UserRepoIntegrationTest {
@Container
static PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:15");

@DynamicPropertySource
static void props(DynamicPropertyRegistry registry) {
[Link]("[Link]", postgres::getJdbcUrl);
}
}

09 DevOps & Infrastructure

9.1 Docker

Docker & Dockerfile


Packages your application and all its dependencies into a portable container image.
📌 Example: Containerize a Spring Boot application.
# Dockerfile
FROM eclipse-temurin:17-jdk-alpine
WORKDIR /app
COPY target/[Link] [Link]
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "[Link]"]

# Build & Run


docker build -t myapp:latest .
docker run -p 8080:8080 myapp:latest

Docker Compose
Defines and runs multi-container applications. Run your app + DB + Redis together.
📌 Example: Run Spring Boot + PostgreSQL + Redis locally.
# [Link]
services:
app:
build: .
ports: ["8080:8080"]
depends_on: [db, redis]
db:
image: postgres:15
environment:
POSTGRES_DB: mydb
POSTGRES_USER: user
POSTGRES_PASSWORD: secret
redis:
image: redis:7-alpine
# Run with: docker-compose up

9.2 CI/CD — GitHub Actions

CI/CD Pipeline
Continuous Integration automatically tests your code on every push. Continuous Delivery
automatically deploys passing code.
📌 Example: Auto-test and build on every PR.
# .github/workflows/[Link]
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with: {java-version: '17', distribution: 'temurin'}
- run: mvn clean test
- run: mvn package -DskipTests
- run: docker build -t myapp .

9.3 Cloud Basics — AWS


Service What it does Java Backend Use Case
EC2 Virtual servers in the cloud Host your Spring Boot app

RDS Managed relational databases Run PostgreSQL/MySQL without managing


servers

S3 Object storage Store user uploads, static files


ElastiCache Managed Redis/Memcached Caching layer for your app

SQS Managed message queue Alternative to RabbitMQ for async jobs

ECS/EKS Container orchestration Deploy Docker containers at scale

API Gateway HTTP API management Entry point, rate limiting, auth

10 Version Control — Git

Core Git Commands


Essential Git operations every developer must know.
📌 Example: Daily workflow: branch, commit, push, PR.
git checkout -b feature/user-auth # new branch
git add . # stage changes
git commit -m "feat: add JWT auth" # commit
git push origin feature/user-auth # push
git pull origin main # update local
git merge feature/user-auth # merge branch
git rebase main # replay commits on top of main

GitFlow Strategy
A branching model with main, develop, feature, release, and hotfix branches.
📌 Example: Develop features in isolation; merge to develop; release to main.
main → production code only
develop → integration branch
feature/xxx → new features branched from develop
release/x.x → final testing before production
hotfix/xxx → urgent production bug fixes

11 System Design Fundamentals

Monolith vs Microservices
Monolith: single deployable unit, simple but hard to scale. Microservices: independent services,
complex but scalable.
📌 Example: Start with a monolith, extract services when a specific domain needs to scale
independently.
Monolith:
[Single Spring Boot App → Single DB]

Microservices:
[User Service] → [User DB]
[Order Service] → [Order DB]
[Payment Service] → [Payment DB]
↓ communicate via REST or Kafka
Circuit Breaker — Resilience4j
Prevents cascading failures by "opening the circuit" when a downstream service fails repeatedly.
📌 Example: If Payment Service is down, fail fast instead of waiting 30s per request.
@CircuitBreaker(name = "paymentService", fallbackMethod = "paymentFallback")
public PaymentResult processPayment(PaymentRequest req) {
return [Link](req);
}

public PaymentResult paymentFallback(PaymentRequest req, Exception e) {


return [Link]("Will retry later");
}

Load Balancing
Distributes incoming traffic across multiple server instances to prevent overload.
📌 Example: NGINX distributes requests across 3 Spring Boot instances.
# [Link]
upstream myapp {
server app1:8080;
server app2:8080;
server app3:8080;
}
server {
location / { proxy_pass [Link] }
}

CAP Theorem
A distributed system can only guarantee 2 of 3: Consistency, Availability, Partition Tolerance.
Networks always partition, so choose CA vs CP.
📌 Example: PostgreSQL is CP (consistent + partition-tolerant). DynamoDB is AP (available +
partition-tolerant).
CP systems: return error if can't guarantee consistency
AP systems: return stale data rather than error

CA would mean: single-node systems (no partition tolerance)→ not realistic in


distributed systems

12 Logging & Monitoring

SLF4J + Logback
SLF4J is a logging facade; Logback is the default implementation in Spring Boot.
📌 Example: Log an error when payment fails.
@Service
public class PaymentService {
private static final Logger log = [Link]([Link]);

public void process(Payment p) {


[Link]("Processing payment for user={}", [Link]());
try {
// ...
[Link]("Payment details: {}", p);
} catch (Exception e) {
[Link]("Payment failed for user={}", [Link](), e);
}
}
}

Spring Boot Actuator + Prometheus + Grafana


Actuator exposes metrics; Prometheus scrapes them; Grafana visualizes dashboards.
📌 Example: Monitor API response times and JVM memory.
# [Link]
management:
endpoints:
web:
exposure:
include: "*"
metrics:
export:
prometheus:
enabled: true

# Access metrics: GET /actuator/prometheus


# Grafana dashboard connects to Prometheus datasource

13 Security Fundamentals

OWASP Top 10 (Key Ones)


Most critical web application security risks every developer must know.
📌 Example: Prevent SQL injection by always using parameterized queries.
// SQL INJECTION - NEVER do this:
String query = "SELECT * FROM users WHERE name = '" + input + "'";

// SAFE - parameterized query:


@Query("SELECT u FROM User u WHERE [Link] = :name")
User findByName(@Param("name") String name);

// XSS - sanitize HTML output


// CSRF - Spring Security handles with CSRF tokens
// Broken Auth - use Spring Security + JWT

Password Hashing — BCrypt


Never store plain text passwords. BCrypt hashes with a salt and work factor to resist brute force.
📌 Example: Hash password on signup, verify on login.
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12); // work factor 12
}

// On signup:
String hashed = [Link](rawPassword);
[Link](hashed);
// On login:
boolean valid = [Link](rawPassword, hashedPassword);

Secrets Management
Never hardcode credentials. Use environment variables, secrets managers, or Vault.
📌 Example: Load DB password from environment variable, not code.
# BAD - never do this:
[Link]=mypassword123

# GOOD - environment variable


[Link]=${DB_PASSWORD}

# BETTER - AWS Secrets Manager / HashiCorp Vault


# spring-cloud-starter-aws-secrets-manager-config

🎯 Your Learning Roadmap


Core Java & Spring → SQL & JPA → REST APIs → Testing → Docker → Git → Messaging →
System Design

You might also like