Spring Boot Fundamentals: Configuration, Integration, and Testing
Spring Boot Core Features
Spring Boot simplifies Spring application development through three key features:
- Auto-configuration: Automatically configures Spring applications based on dependencies present
- Starter Dependencies: Bundles common dependencies for specific functionalities
- Production-ready Featrues: Includes metrics, health checks, and externalized configuration
Building a Basic Spring Boot Application
Implementation Steps:
- Create Maven project with parent POM:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
</parent>
- Add web starter dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- Create controller:
@RestController
public class GreetingController {
@GetMapping("/greet")
public String greet() {
return "Welcome to Spring Boot";
}
}
- 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:
.properties.yml.yaml
YAML Syntax:
app:
server:
port: 9090
host: localhost
features: [logging, metrics]
Accessing Configuration:
- Using @Value:
@Value("${app.server.port}")
private int serverPort;
- Using Environment:
@Autowired
private Environment env;
String host = env.getProperty("app.server.host");
- Using @ConfigurationProperties:
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private Server server;
private List<String> features;
// Getters and setters
}
Profile Management
Profile Activation Methods:
- Configuration file:
spring.profiles.active=prod
- Command line:
java -jar app.jar --spring.profiles.active=dev
Integration Testing
Test Setup:
- Add test dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- Create test class:
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
// Test logic
}
}
Database Integration
MyBatis Configuration:
- Add dependencies:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
- Configure datasource:
spring:
datasource:
url: jdbc:mysql://localhost:3306/db
username: user
password: pass
- Create repository:
@Mapper
public interface UserRepository {
@Select("SELECT * FROM users")
List<User> findAll();
}
Redis Integration
- Add Redis starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- Configure Redis:
spring:
redis:
host: localhost
port: 6379
- Use RedisTemplate:
@Autowired
private RedisTemplate<String, String> redisTemplate;
public void storeData(String key, String value) {
redisTemplate.opsForValue().set(key, value);
}