πŸ”Ή 1. SpEL (Spring Expression Language)

βœ… Definition:

SpEL is a powerful expression language used in Spring for querying and manipulating object graphs at runtime. It’s commonly used inside @Value, XML, or annotation-based configuration to dynamically inject values.

βœ… Basic Example:

@Component
public class MyComponent {

@Value("#{2 * 2}")
private int result;

@Value("#{T(Math).sqrt(25)}")
private double squareRoot;

@Value("#{myBean.name}")
private String injectedName;
}

Assume myBean is another Spring-managed bean:

@Component("myBean")
public class MyBean {
public String name = "Ved";
}

βœ… Explanation:

  • @Value("#{2 * 2}") β†’ Injects the result 4.
  • T(Math).sqrt(25) β†’ Calls Math.sqrt(25) β†’ Result: 5.0.
  • #{myBean.name} β†’ Injects the name property from the bean named myBean.

βœ… Use Cases:

  • Dynamic default values
  • Reading system properties
  • Accessing other beans’ fields or methods

πŸ”Ή 2. @Scheduled (Task Scheduling)

βœ… Definition:

Spring’s @Scheduled annotation allows you to execute methods on a schedule, like a cron job or fixed interval.

βœ… Example:

@Component
public class MyScheduler {

// Runs every 5 seconds
@Scheduled(fixedRate = 5000)
public void poll() {
System.out.println("Polling at " + new Date());
}
}

βœ… Enable Scheduling:

To make @Scheduled work, add this to a config class:

@Configuration
@EnableScheduling
public class AppConfig {}

βœ… Explanation:

  • fixedRate = 5000 β†’ Runs the method every 5 seconds, regardless of previous execution time.
  • Alternative attributes:
    • fixedDelay = 5000: Runs after the previous call is completed + 5 seconds.
    • cron = "0 0/1 * * * *": Runs every 1 minute (cron format).

βœ… Use Cases:

  • Polling APIs
  • Auto-backups
  • Regular cleanup tasks

πŸ”Ή 3. @Cacheable (Caching Results)

βœ… Definition:

@Cacheable marks a method so that Spring will cache the result of the method. If the same method is called with the same arguments again, Spring returns the cached result instead of executing the method again.

βœ… Example:

@Service
public class UserService {

@Cacheable("users")
public User findById(String id) {
simulateSlowService();
return new User(id, "Ved");
}

private void simulateSlowService() {
try {
Thread.sleep(3000); // Simulate slow DB call
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

βœ… Enable Caching:

@Configuration
@EnableCaching
public class CacheConfig {
// Configure your cache manager (e.g., ConcurrentMapCacheManager)
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("users");
}
}

βœ… Explanation:

  • First call to findById("123") β†’ Takes 3 seconds and result is cached.
  • Second call to findById("123") β†’ Returns immediately from cache.

βœ… Use Cases:

  • Reducing DB calls
  • Caching expensive calculations
  • Performance optimization for read-heavy apps

πŸ”š Summary Table:

ConceptPurposeKey Annotation
SpELDynamically inject values@Value("#{...}")
@ScheduledRun methods periodically@Scheduled
@CacheableCache method return values@Cacheable

Similar Posts