@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"); } }
@PostMapping @ResponseStatus(HttpStatus.CREATED) public User create(@Valid @RequestBody User user) { return userService.save(user); } spring boot in action
myapp: custom-property: value @ConfigurationProperties(prefix = "myapp") @Component public class AppProperties { private String customProperty; private EmailConfig email = new EmailConfig(); // getters/setters @Data public static class EmailConfig { private String host; private int port; } } private EmailConfig email = new EmailConfig()
@Service public class UserService { @Cacheable(value = "users", key = "#id") public User findById(Long id) { return userRepository.findById(id).orElseThrow(); } private int port
@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 { }
@Scheduled(fixedDelay = 5000) public void fixedDelayTask() { // Runs 5 seconds after previous execution }
@Scheduled(initialDelay = 10000, fixedRate = 30000) public void fixedRateTask() { } } @Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(10); executor.setQueueCapacity(100); executor.initialize(); return executor; } }