• Register
300 points
6 1 1

Spring framework annotations @Autowire @Component @Bean  @Configuration @Value

So let’s see some basic core annotation will be used in every project.

 

@Autowire 

The @Autowired annotation is the most used way to inject the dependencies in Spring. By default, the @Autowired resolve dependencies by type. This works fine until we have only one bean with the same type.But the Spring framework will throw an exception if there are two beans with same type is available in the container. The @Autowired annotation will not work in this case. Because spring doesnt know which beans needs to autowire there.

 

There are two ways to Autowire the beans :

  • Autowire by type
  • Autowire by name

 

Autowiring by type: @Autowired by type uses the class type to autowire the spring boot bean class. The bean is autowired based on the type of the variable.

Autowiring by name: For Autowiring by name, the name of the variable is used for the dependency injection.

Note :-  You can inject only beans by autowire.

@Component

 

Component is stereo type annotation which is used to declare a bean. When you annotation your java class with @Component bean will be created at the time of compilation of your program and now you can inject by Autowiring.

 

@Configuration

Spring @Configuration annotation is used to declare more than one bean in a class with the help of @Bean annotation. @Configuration annotation is a class-level annotation and normally in a configuration bean it contains 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.

 

Use @Configuration annotation on 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.

 

@Bean

This annotation is used at the method level. @Bean the annotation works with @Configuration to create Spring beans. As mentioned earlier, @Configuration will have methods to instantiate and configure dependencies. Such methods will be annotated with @Bean. We already saw the examples of @Bean annotation as part of @Configuration annotation examples.

 

@Value

@Value is used to access the property files values or you can directly set the value of that property directly in @Value.

 

Thanks you can also visit other spring boot blog here : –

 

https://kodlogs.com/blog/9239/spring-bean-life-cycle-and-creation-of-bean-with-bean-scope

 

 

3 Comments

On the internet, you never know what you'll find. It's full of surprises, just like this incredible blog.
Thanks a lot, Darshan. This is a very needy post. ^_^
Please expand on this subject. Your ability to explain things is wonderful.

More Posts

REST APIs / Services with Spring boot darshan1507 - Jun 5
Create a Web application in Spring boot with PostgreSQL (Part-3) darshan1507 - May 25
Uploading files to AWS S3 Bucket using Spring Boot darshan1507 - Jun 7
Application Properties in Spring Boot thistimeonly32 - Apr 4
Spring AOP Interview Questions Lalit Kumar - Dec 30, 2020
Difference between a framework and a library amna - Mar 16
Type Annotations in Swift Tomas Herman - 1 day
Create a Web application in Spring boot with PostgreSQL (Part-2) darshan1507 - May 25
Create a Web application in Spring boot with PostgreSQL (Part-1) darshan1507 - May 25
Spring Bean Life Cycle and creation of Bean with Bean Scope ........ darshan1507 - May 22