Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Spring Boot Fundamentals: Configuration, Integration, and Testing

Tech May 12 2

Spring Boot Core Features

Spring Boot simplifies Spring application development through three key features:

  1. Auto-configuration: Automatically configures Spring applications based on dependencies present
  2. Starter Dependencies: Bundles common dependencies for specific functionalities
  3. Production-ready Featrues: Includes metrics, health checks, and externalized configuration

Building a Basic Spring Boot Application

Implementation Steps:

  1. Create Maven project with parent POM:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>
  1. Add web starter dependency:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  1. Create controller:
@RestController
public class GreetingController {
    @GetMapping("/greet")
    public String greet() {
        return "Welcome to Spring Boot";
    }
}
  1. Create main appplication class:
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Configuration Management

Spring Boot supports multiple configuration formats with following priorities:

  1. .properties
  2. .yml
  3. .yaml

YAML Syntax:

app:
  server:
    port: 9090
    host: localhost
  features: [logging, metrics]

Accessing Configuration:

  1. Using @Value:
@Value("${app.server.port}")
private int serverPort;
  1. Using Environment:
@Autowired
private Environment env;

String host = env.getProperty("app.server.host");
  1. Using @ConfigurationProperties:
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private Server server;
    private List<String> features;
    // Getters and setters
}

Profile Management

Profile Activation Methods:

  1. Configuration file:
spring.profiles.active=prod
  1. Command line:
java -jar app.jar --spring.profiles.active=dev

Integration Testing

Test Setup:

  1. Add test dependencies:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  1. Create test class:
@SpringBootTest
class ApplicationTests {
    @Test
    void contextLoads() {
        // Test logic
    }
}

Database Integration

MyBatis Configuration:

  1. Add dependencies:
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
</dependency>
  1. Configure datasource:
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db
    username: user
    password: pass
  1. Create repository:
@Mapper
public interface UserRepository {
    @Select("SELECT * FROM users")
    List<User> findAll();
}

Redis Integration

  1. Add Redis starter:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. Configure Redis:
spring:
  redis:
    host: localhost
    port: 6379
  1. Use RedisTemplate:
@Autowired
private RedisTemplate<String, String> redisTemplate;

public void storeData(String key, String value) {
    redisTemplate.opsForValue().set(key, value);
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.