Spring Boot In Action |link| -
// Activate profile // --spring.profiles.active=dev,swagger Logback Configuration <!-- logback-spring.xml --> <configuration> <springProfile name="dev"> <root level="DEBUG"/> </springProfile> <springProfile name="prod"> <root level="INFO"/> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>logs/app.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>logs/app-%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> </appender> </springProfile> </configuration> Structured Logging @Slf4j @Service public class OrderService { public void processOrder(Order order) { log.info("Processing order: {}", order.getId()); MDC.put("orderId", order.getId().toString()); try { // business logic } finally { MDC.clear(); } } } 10. Caching Enable Caching @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { return new ConcurrentMapCacheManager("users", "products"); } }
@CacheEvict(value = "users", key = "#user.id") public User update(User user) { return userRepository.save(user); } spring boot in action
@JsonTest class UserJsonTest { } Profile Configuration # application-dev.yml spring: datasource: url: jdbc:h2:mem:testdb jpa: show-sql: true application-prod.yml spring: datasource: url: ${DATABASE_URL} hikari: maximum-pool-size: 20 Using Profiles @Configuration @Profile("dev") public class DevConfig { } @Service @Profile("!test") public class ProductionService { } // Activate profile // --spring
// Enable configuration properties @EnableConfigurationProperties(AppProperties.class) Spring Data JPA @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(unique = true) private String email; @CreationTimestamp private LocalDateTime createdAt; } // Activate profile // --spring.profiles.active=dev
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoggingInterceptor()) .addPathPatterns("/api/**"); } } @RestController public class ReactiveUserController { @GetMapping("/flux/users") public Flux<User> getAllReactive() { return userReactiveRepository.findAll() .delayElements(Duration.ofSeconds(1)); }
@Service public class EmailService { @Async public CompletableFuture<Void> sendEmail(String to, String content) { // Simulate email sending return CompletableFuture.completedFuture(null); } } RabbitMQ @Configuration public class RabbitConfig { @Bean public Queue queue() { return new Queue("order.queue", false); } @Bean public TopicExchange exchange() { return new TopicExchange("order.exchange"); } }
@ExceptionHandler(UserNotFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public ErrorResponse handleNotFound(UserNotFoundException ex) { return new ErrorResponse(ex.getMessage()); } } @Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://localhost:3000") .allowedMethods("GET", "POST", "PUT", "DELETE"); }