Description: this article is talking about some annotations in Spring.
Pre class
IoC Containers
The Spring container is at the core of the Spring Framework. The container will create the objects, wire them together, configure them, and manage their complete life cycle from creation till destruction. The Spring container uses DI to manage the components that make ip an appliation. These objects are called Spring Beans
What is Spring Bean
The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container
@Autowired
this annotation is making a dependency which Spring is going to resolve and inject. We can use this annotation with a constructor, setter, or field injection.
it has a boolean argument called reuqired with a default value of true. It tunes Spring's behavior when it doesn't find a suitable bean to write. When true, an exception is thrown, otherwise, nothing is wired.
1 |
|
If we use constructor injection, all constructor arguments are mandatory.
- Constructor injection
1 | class Car { |
@Bean
makes a factory method which instantiates a Spring Bean: it is a method-level annotation. It is an alternative of XML <bean> tag. It tells the method to produce a bean to be managed by Spring Container
1 |
|
Spring calls these methods when a new instance of the return type is required.
The resulting bean has the same name as the factory method. If we want to name it differently, we can do so with the name or the value arguments of this annotation(the argument value is an alias for the arguments name)
1 |
|
**Note: all methods annotated with @Bean must be in @Configuration classes.**
@Required
it on setter methdos to mark dependencies that we want to populate through XML
1 |
|
1 | <bean class="com.baeldung.annotations.Bike"> |
@Component
it is class-level annotation. It is used to mark a Java class as a bean. A java class annotated with @Component is found during the classpath. The spring framework pick it up and configure it in the application context as a Spring bean.
1 |
|
@Controller
the @Controller is a class-level annotation. It is a specializaton of @Component. It makes a class a web request handler. It is often used to serve web pages. By default, it returns a string that indicates which route to redirect. It is mostly used with @RequestMapping annotation.
1 |
|
@Service
it is a class level, it tells the Spring that class contains the business logic
1 |
|
@Repository
it is a class-level annotation. The repository is a DAOs(Data Access Object) that access the database directly. The repository does all the operations related to the database
1 | //responsible for data access |
Configuration
is the top of any class to declare that this class provides one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime
1 |
|