MTI TEK
Spring Framework
|
Spring Core Concepts
The Spring Application Context
- Spring offers a container called the Spring Application Context — it creates, manages, and wires together application components called beans.
- Beans are wired together inside the application context via Dependency Injection (DI): the container injects dependent beans through constructor arguments or property accessor methods.
Component Scanning & Autowiring
- Component scanning: Spring automatically discovers components from the application's classpath and registers them as beans in the application context.
- Autowiring: Spring automatically injects beans with the other beans they depend on.
- Classes annotated with
@Component, @Controller, @Service, or @Repository are automatically discovered and registered as beans when component scanning is active.
Key Annotations
@Configuration
- Marks a class as a Spring configuration class — it provides bean definitions to the application context.
- Methods inside a
@Configuration class can be annotated with @Bean to declare beans.
- Bean methods in a
@Configuration class run in full mode (CGLIB proxy) — inter-bean method calls are intercepted by Spring, ensuring the singleton contract is enforced.
@Bean
- Applied to a method in any Spring-managed class — the object returned by the method is registered as a bean in the application context.
- The bean ID defaults to the method name.
- When used inside a
@Configuration class (full mode): the method is proxied by Spring — calling it multiple times always returns the same singleton instance.
- When used inside a
@Component or other scanned class (lite mode): the method is a plain Java call — no proxy, no interception. Calling the method directly from another method creates a new instance each time instead of reusing the bean.
// Full mode — inside @Configuration (proxied)
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
// Lite mode — inside @Component (not proxied)
@Component
public class AppComponents {
@Bean
public MyService myService() {
return new MyService();
}
}
@Component, @Controller, @Service, @Repository
@Component: generic stereotype — marks a class for component scanning; the class is instantiated and registered as a bean.
@Controller: specialized form of @Component — used for web layer classes.
@Service: specialized form of @Component — used for service layer classes.
@Repository: specialized form of @Component — used for data access layer classes.
@ComponentScan
- Enables component scanning — Spring scans the package of the annotated class and all sub-packages.
- By default, the scan roots at the package of the class carrying
@ComponentScan — place the main application class at the top-level package to ensure all sub-packages are covered.
@SpringBootConfiguration
- A specialized form of
@Configuration — designates the class as a Spring Boot configuration class.
- Only one
@SpringBootConfiguration (or @SpringBootApplication) should exist per application.
@EnableAutoConfiguration
- Enables Spring Boot's automatic configuration mechanism — Spring Boot makes reasonable guesses at what needs to be configured based on classpath entries, environment variables, and other factors.
@SpringBootApplication
- Combines
@SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan.
- Designates the main bootstrap class of a Spring Boot application.
- The
main() method calls SpringApplication.run() to bootstrap the application context.
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
@SpringBootTest
- Tells JUnit to bootstrap the test with Spring Boot capabilities — loads the full Spring application context for the test.
- If the application context fails to start, the test fails.
@SpringBootTest
class MyApplicationTests {
@Test
void contextLoads() { }
}