βš™οΈ Spring Core Quick-Starter β€” All-in-One Cheatsheet

Essence: Everything that powers Spring β€” from reflection and beans to IoC, lifecycle, AOP, and events β€” condensed for daily reference. Think of it as your cockpit dashboard: every switch labeled, no theory required.


🌱 1. Reflection β€” How Spring Sees Your Code

Core Idea

Spring uses Java reflection to discover, instantiate, and wire classes at runtime.

Java Basics

Class<?> c = PaymentService.class;
Object o = c.getDeclaredConstructor().newInstance();
Field f = c.getDeclaredField("audit");
f.setAccessible(true);
f.set(o, new AuditService());

In Spring

Operation Reflection Use
Classpath scanning Class.forName()
Bean creation Constructor.newInstance()
Field injection Field.set()
Lifecycle methods Method.invoke()

Key Annotation

@Autowired β€” tells Spring to inject a dependency automatically.


🧩 2. Bean Anatomy β€” What Spring Manages

Definition

A bean is any Java object managed by Spring β€” created, wired, and destroyed inside the ApplicationContext.

Lifecycle Overview

instantiate β†’ inject β†’ initialize β†’ active β†’ destroy

Lifecycle Hooks

Stage Hook Description
Initialization @PostConstruct Runs after dependencies injected
Initialization afterPropertiesSet() From InitializingBean
Destruction @PreDestroy Runs before context shutdown
Destruction destroy() From DisposableBean

Core Annotations

Annotation Role
@Component Marks a Spring bean
@Service Specialized component for service layer
@Repository DAO-level component, exception translation
@Configuration Source of @Bean definitions
@Bean Defines method-produced beans

🧠 3. Dependency Injection β€” How Beans Connect

Injection Styles

Type Example Notes
Constructor public Service(Repo repo) Preferred β€” immutable
Setter @Autowired setRepo(Repo r) Optional or late binding
Field @Autowired private Repo repo; Simple, less testable

Qualifiers

Annotation Purpose
@Qualifier("beanName") Choose specific bean
@Primary Default bean for a type
@Profile("dev") Load only for active profile

Optional/Lazy

@Autowired(required = false)
@Lazy
private ExpensiveService heavy;

Spring injects a proxy, creates bean only when used.


🧬 4. IoC Container β€” Who Controls the Show

Philosophy

Inversion of Control (IoC) means the framework controls object creation, not you.

Core Interfaces

Interface Purpose
BeanFactory Minimal container for DI
ApplicationContext Full-featured container with events, i18n, resources

Common Context Classes

Class Usage
AnnotationConfigApplicationContext Plain Java config
ClassPathXmlApplicationContext Legacy XML config
SpringApplication Boot entry point

Useful Methods

ctx.getBean(MyService.class);
ctx.containsBean("auditService");
ctx.getEnvironment().getActiveProfiles();
ctx.close();

BeanDefinition Metadata

Property Meaning
beanClass Actual Class<?>
scope singleton / prototype / request
initMethod Name of init method
destroyMethod Cleanup method

🧭 5. Application Context Lifecycle β€” From Start to Stop

Spring Boot run()
  ↓
create ApplicationContext
  ↓
scan classpath
  ↓
register BeanDefinitions
  ↓
instantiate + inject
  ↓
post-process + initialize
  ↓
publish ContextRefreshedEvent
  ↓
ready to serve
  ↓
(ContextClosedEvent β†’ destroy hooks)

Key Events

Event When Triggered
ContextRefreshedEvent All beans initialized
ContextStartedEvent Context started (rarely used)
ContextClosedEvent Context shutting down
ContextStoppedEvent Graceful stop
ApplicationReadyEvent Boot app fully started

βš™οΈ 6. AOP β€” How Spring Adds Behavior Dynamically

Core Idea

Aspect-Oriented Programming (AOP) lets Spring weave extra behavior (transactions, caching, logging) around method calls.

Key Components

Term Meaning
Aspect Class containing crosscutting logic
Advice Code to run before/after methods
Pointcut Rule for selecting target methods
Proxy Wrapper intercepting method calls

Common Annotations

Annotation Role
@Aspect Declares an aspect class
@Before("execution(...)") Run before method
@After("execution(...)") Run after method
@Around("execution(...)") Wrap method (full control)
@AfterReturning / @AfterThrowing Handle success/error

Proxy Types

Type Mechanism Applies To
JDK Dynamic Proxy java.lang.reflect.Proxy Interfaces
CGLIB Proxy Bytecode subclass Concrete classes

Built-in Use Cases

Feature Annotation Behavior
Transactions @Transactional Begin/commit/rollback
Async @Async Run in background thread
Caching @Cacheable Store return values
Security @PreAuthorize Access control checks

πŸ“‘ 7. Events β€” How Spring Talks Internally

Publish & Listen

@Component
public class AppReadyListener implements ApplicationListener<ApplicationReadyEvent> {
    public void onApplicationEvent(ApplicationReadyEvent e) {
        System.out.println("App started!");
    }
}

Or annotation-style:

@EventListener
public void handle(ContextClosedEvent e) {
    System.out.println("Context closed.");
}

Custom Events

class OrderCreatedEvent extends ApplicationEvent { ... }

ctx.publishEvent(new OrderCreatedEvent(this));

🧩 8. Common Gotchas

Problem Cause Fix
NoSuchBeanDefinitionException Missing or mismatched type Add @Component or correct @Qualifier
BeanCurrentlyInCreationException Circular constructor injection Break cycle with setter injection
@PostConstruct not called Method not public void / wrong signature Fix method definition
AOP not triggered Self-invocation inside same class Move call to another bean

πŸ” 9. Useful Debugging Tricks

List all beans:

Arrays.stream(ctx.getBeanDefinitionNames()).forEach(System.out::println);

Trace events:

@Component
public class EventsTracer implements ApplicationListener<ApplicationEvent> {
    public void onApplicationEvent(ApplicationEvent e) {
        System.out.println("Event β†’ " + e.getClass().getSimpleName());
    }
}

Inspect proxy target:

AopUtils.getTargetClass(bean);

🧬 10. Hierarchy Map

[Reflection Layer]
   β”œβ”€ Reflection API
   └─ @Autowired injection
        ↓
[Beans Layer]
   β”œβ”€ Bean annotations & lifecycle
   └─ BeanDefinition metadata
        ↓
[Container Layer]
   β”œβ”€ Dependency injection & ApplicationContext
   └─ BeanFactory, IoC flow
        ↓
[AOP Layer]
   β”œβ”€ Aspects & proxies
        ↓
[Events Layer]
   └─ Application events & listeners

πŸͺž Core Takeaway

Spring turns static Java code into a living ecosystem. Reflection lets it see your classes, IoC lets it control their creation, AOP lets it enrich their behavior, and the event system lets it coordinate the whole show.