๐Why Learn Spring (Not Just Spring Boot)?
Everyone jumps straight to Spring Boot and then gets confused because they never learned what Boot is built *on*. Spring Boot is a convenience layer; the Spring Framework is the engine, its Inversion of Control container, dependency injection, and AOP are what make the whole ecosystem work. Understand these once and Spring Boot, Spring Data, and Spring Security all suddenly make sense.
This tutorial covers the four pillars every beginner needs: the IoC container, dependency injection, AOP, and Spring MVC. When you are ready to go faster, the Spring Boot course and the Spring Boot track build directly on this.
๐Pillar 1: Inversion of Control (IoC)
Normally your code creates its own dependencies:
// Tightly coupled, YOU control creationpublic class OrderService { private final PaymentService payment = new UpiPaymentService(); // hardcoded!}The problem: OrderService is welded to UpiPaymentService. You cannot swap it, and you cannot unit test with a fake. Inversion of Control flips this, instead of your class creating dependencies, a container creates them and hands them in. The control is *inverted* from your code to the framework.
๐Pillar 2: Dependency Injection (DI)
DI is *how* IoC is implemented, the container "injects" dependencies. Prefer constructor injection:
@Servicepublic class OrderService { private final PaymentService payment; // Spring injects the right PaymentService here public OrderService(PaymentService payment) { this.payment = payment; } public void placeOrder(int amount) { payment.pay(amount); }}Now OrderService depends on the PaymentService interface, not a concrete class. Swap implementations without editing OrderService, and inject a mock in tests trivially. This single idea is the backbone of testable Java. If interfaces and objects feel shaky, shore them up via the Java course.
๐Beans and the Application Context
A bean is just an object managed by the Spring container. The container, the ApplicationContext, creates beans, wires their dependencies, and manages their lifecycle. You declare beans with stereotype annotations:
@Component, a generic Spring-managed bean@Service, business logic@Repository, data access (adds exception translation)@Controller / @RestController, web layerAnd you tell Spring where to look with configuration:
@Configuration@ComponentScan("com.prakalpana.shop")public class AppConfig { // An explicitly declared bean @Bean public PaymentService paymentService() { return new UpiPaymentService(); }}// Bootstrapping the container manually (what Spring Boot hides from you)ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);OrderService orders = ctx.getBean(OrderService.class);orders.placeOrder(500);๐Bean Scopes and Lifecycle
By default beans are singletons, one shared instance per container. Other scopes include prototype (a new instance each time), and web scopes request and session. You can hook into the lifecycle with @PostConstruct and @PreDestroy:
@Componentpublic class CacheWarmer { @PostConstruct public void init() { System.out.println("Bean ready, warming cache..."); } @PreDestroy public void cleanup() { System.out.println("Container shutting down, flushing cache..."); }}๐Pillar 3: Aspect-Oriented Programming (AOP)
Some concerns, logging, security, transactions, cut *across* many methods. Copying that code everywhere is a mess. AOP lets you write it once as an aspect and apply it declaratively. Key terms: an aspect is the module, a join point is where it can run, a pointcut selects which join points, and advice is the code that runs.
@Aspect@Componentpublic class LoggingAspect { // Run before every method in the service package @Before("execution(* com.prakalpana.shop.service.*.*(..))") public void logCall(JoinPoint jp) { System.out.println("Calling: " + jp.getSignature().getName()); }}This is exactly how @Transactional works under the hood, Spring wraps your method in a proxy that opens and commits a transaction around it. Understanding AOP demystifies a lot of "magic."
๐Pillar 4: Spring MVC
Spring MVC is the web layer. A request flows through the DispatcherServlet, which routes it to the right controller method, which returns data (or a view name). The core annotations:
@RestController@RequestMapping("/api/products")public class ProductController { private final ProductService service; public ProductController(ProductService service) { this.service = service; } @GetMapping("/{id}") public Product getOne(@PathVariable Long id) { return service.findById(id); } @PostMapping public Product create(@RequestBody Product p) { return service.save(p); }}@RestController combines @Controller and @ResponseBody, so return values are serialized straight to JSON, no view resolver needed for APIs.
๐Where Spring Boot Fits
Everything above still needs manual configuration, a servlet container, component scanning, JSON setup. Spring Boot auto-configures all of it and embeds a Tomcat server so you can run a JAR directly. But now you know what it is automating. For the full picture of the difference, read our comparison and then move to the Spring Boot track.
๐A Beginner's Learning Order
@Transactional stops being magic๐Common Beginner Questions
@Component is auto-detected via scanning; @Bean is declared explicitly in a @Configuration class, use it for objects you do not own.๐Learn It With a Mentor
Spring's concepts click fastest when someone explains the *why* and reviews your code live. Prakalpana offers live online and 1-on-1 Spring and Spring Boot training that starts from exactly these fundamentals. WhatsApp or call +91 9945619267 for a free trial session.