Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
Spring Framework provides a rich set of annotations, which can be categorized based on their use-cases. The core annotations primarily fall under the following categories:
Core & Stereotype Annotations:
@Component
: Marks a class as a component. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.@Repository
: Marks a class as a DAO (Data Access Object) component in the persistence layer. It's a specialization of @Component
.@Service
: Indicates a service component in the business layer. Another specialization of @Component
.@Controller
: Designates a class as a Spring MVC controller.@RestController
: A specialized version of @Controller
that assumes @ResponseBody
semantics on every method.Dependency Injection Annotations:
@Autowired
: Marks a constructor, field, setter method, or configuration method as to be autowired by Spring's dependency injection.@Inject
: Java standard annotation with semantics similar to @Autowired
.@Resource
: Used to wire a particular resource by name.@Qualifier
: Provides finer-grained control over where and how autowiring should be done.@Value
: Indicates a default value for a field or parameter, typically used for expression-driven dependency injection.@Required
: Indicates that a bean property must be populated at configuration time.Configuration & Bean Annotations:
@Configuration
: Indicates that a class provides @Bean
definitions to the Spring container.@Bean
: Annotated methods within a @Configuration
class will be used as definitions for creating beans.@ComponentScan
: Configures the base packages for component scanning.@Import
: Allows for loading @Bean
definitions from another configuration class.@PropertySource
: Specifies the location of property files.Aspect-Oriented Annotations:
@Aspect
: Declares a class as an aspect in Aspect Oriented Programming.@Pointcut
: Declares a pointcut expression.@Before
, @After
, @Around
, etc.: Used to specify advices in aspect-oriented programming.Transaction Annotations:
@Transactional
: Specifies that a method or class is transactional.MVC Annotations:
@RequestMapping
: Maps a specific URL or HTTP method to a method or a class in a controller.@GetMapping
, @PostMapping
, @PutMapping
, etc.: Shorthand versions of @RequestMapping
for specific HTTP methods.@ResponseBody
: Indicates that a method return value should be bound to the web response body.@PathVariable
, @RequestParam
, @RequestBody
, etc.: Annotations to bind request parameters, path variables, or body to method parameters.Lifecycle & Scope Annotations:
@PostConstruct
& @PreDestroy
: Lifecycle callbacks.@Scope
: Configures the scope of a bean, e.g., prototype, singleton, etc.Testing Annotations:
@Test
: Marks a method as a test method (from JUnit).@SpringBootTest
: Denotes that the annotated class is a Spring Boot test.@MockBean
: Allows mocking a bean in Spring's application context for testing.This is just a basic overview. The actual set of available annotations might vary based on the specific modules of Spring you are using (e.g., Spring Data, Spring Security, etc.). Always refer to the Spring documentation or the Javadoc for a specific version to get a comprehensive list and understanding of annotations.
Spring @Autowired annotation usage:
@Autowired
annotation is used for automatic dependency injection. It can be applied to fields, setter methods, and constructors.// MyService.java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final MyRepository repository; @Autowired public MyService(MyRepository repository) { this.repository = repository; } // Other methods... }
How to use @Component annotation in Spring:
@Component
annotation is a generic stereotype annotation indicating that the class is a Spring component.// MyComponent.java import org.springframework.stereotype.Component; @Component public class MyComponent { // Class implementation... }
Exploring @Service annotation in Spring:
@Service
annotation is a specialization of @Component
used to annotate service classes.// MyService.java import org.springframework.stereotype.Service; @Service public class MyService { // Service implementation... }
Usage of @Configuration annotation in Spring:
@Configuration
annotation is used to define a configuration class that can declare one or more @Bean
methods.// AppConfig.java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } // Other configuration methods... }