Skip to content

martinzachariassen/spring-lab

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Spring Lab

A minimalist Spring Boot learning project designed for exploring Spring features, implementing POCs, and hands-on learning through feature development.

Overview

This project serves as a sandbox for experimenting with Spring Boot capabilities. The simple, clean structure allows you to focus on learning and implementing features without unnecessary boilerplate or complexity.

Project Structure

src/
β”œβ”€β”€ main/
β”‚   β”œβ”€β”€ java/no/mlz/springlab/
β”‚   β”‚   └── SpringlabApplication.java    # Main application entry point
β”‚   └── resources/
β”‚       β”œβ”€β”€ application.yml              # Spring configuration
β”‚       └── logback-spring.xml           # Logging configuration
└── test/
    └── java/no/mlz/springlab/
        └── SpringlabApplicationTests.java

Features

Built-in Capabilities

  • Spring Boot Auto-Configuration: Sensible defaults for rapid development
  • H2 In-Memory Database: Pre-configured for quick database testing and learning
    • Web console available at http://localhost:8080/h2-console
    • Auto-creates schema on startup, drops on shutdown
    • Perfect for POCs and learning JPA/Hibernate
  • Comprehensive Logging:
    • Console output with color-coded log levels
    • Rolling file appenders (main, debug, error logs)
    • Async logging for non-blocking file writes
    • Configurable log levels per package

Logging Configuration

The project includes three separate log files:

  • spring-lab.log: General application logs
  • spring-lab-debug.log: Detailed debug information with line numbers
  • spring-lab-error.log: Error-only logs

Log rotation is configured with:

  • Max file size: 10MB
  • Max history: 30 days
  • Total size cap: 1-2GB

Getting Started

Prerequisites

  • Java 11+ (verify with java -version)
  • Maven 3.6+ (verify with mvn -version)

Running the Application

# Build the project
mvn clean install

# Run the application
mvn spring-boot:run

Running Tests

mvn test

Configuration

Application Configuration

Application settings can be modified in src/main/resources/application.yml:

spring:
  application:
    name: spring-lab

  datasource:
    url: jdbc:h2:mem:springlab_db
    driverClassName: org.h2.Driver
    username: sa
    password:

  h2:
    console:
      enabled: true
      path: /h2-console

  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: create-drop  # Options: create-drop, create, update, validate, none
    show-sql: false
    properties:
      hibernate:
        format_sql: true
        use_sql_comments: true

logging:
  level:
    root: INFO
    no.mlz.springlab: DEBUG

H2 Database Console

The H2 database console is available at http://localhost:8080/h2-console when the application is running.

Connection Details:

  • JDBC URL: jdbc:h2:mem:springlab_db
  • Username: sa
  • Password: (leave blank)

The in-memory database is created on startup and destroyed on shutdown, making it perfect for testing and learning.

Learning Paths

Here are some suggested areas to explore:

1. REST APIs

  • Create a controller package and implement @RestController endpoints
  • Practice @GetMapping, @PostMapping, request validation

2. Data Persistence

  • Add Spring Data JPA dependency
  • Create entity classes and repositories
  • Experiment with query methods

3. Service Layer

  • Create a service package with business logic
  • Practice @Service and @Transactional annotations

4. Configuration Management

  • Use @Configuration and @Bean for custom configurations
  • Explore profiles (application-dev.yml, application-prod.yml)

5. Error Handling

  • Implement @RestControllerAdvice for global exception handling
  • Create custom exceptions

Suggested Package Structure (for future expansion)

no.mlz.springlab/
β”œβ”€β”€ controller/      # REST endpoints
β”œβ”€β”€ service/         # Business logic
β”œβ”€β”€ repository/      # Data access
β”œβ”€β”€ entity/          # JPA entities
β”œβ”€β”€ dto/             # Data transfer objects
β”œβ”€β”€ exception/       # Custom exceptions
β”œβ”€β”€ config/          # Configuration classes
└── util/            # Utility classes

Dependencies

Current dependencies (from pom.xml):

  • Spring Boot Starter Web
  • Spring Boot Starter Data JPA
  • H2 Database (runtime)
  • Spring Boot Starter Test

To add new dependencies, edit pom.xml and run mvn clean install.

Logging Best Practices

When implementing features, use logging effectively:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
    
    public void doSomething() {
        logger.debug("Entering doSomething");
        logger.info("Processing request");
        logger.warn("This is a warning");
        logger.error("An error occurred", exception);
    }
}

Debugging

Application logs are available in the logs/ directory. Monitor them during development:

# Watch main log in real-time
tail -f logs/spring-lab.log

# View debug logs
tail -f logs/spring-lab-debug.log

# View error logs
tail -f logs/spring-lab-error.log

Build and Deployment

Build JAR

mvn clean package
java -jar target/springlab-0.0.1-SNAPSHOT.jar

Run with Different Profiles

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

Tips for Learning

  1. Start Small: Implement one feature at a time
  2. Use Logging: Leverage the configured logging to understand flow
  3. Write Tests: Test-driven development helps solidify understanding
  4. Check Logs: Always check the debug and error logs for insights
  5. Experiment: Use POCs to try new patterns and approaches

Resources

License

This is a personal learning project.

About

A Spring Boot learning repo where I intentionally summon edge-cases: self-invocation, proxy magic, transactional rollback surprises, and async timing bugs. If it breaks, it’s a feature β˜•οΈ 🧠

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages