Using @Autowired and @Bean annotations in Spring Boot
In Spring Boot, Dependency Injection (DI) is done through@Autowired
Implemented by annotations, it can effectively simplify the dependencies between objects. At the same time, use@Bean
Annotations can help us explicitly define and initialize beans in configuration classes. This article will use a specific example to demonstrate how to use it in Spring Boot@Autowired
and@Bean
To manage the bean.
Sample background
Suppose we have oneStudent
class, and want to configure the classTestConfig
To initializeStudent
Object, then passed in the test class@Autowired
Annotations automatically inject and use it.
1. Define the Student class
First, we define a simpleStudent
Class, use@Data
Annotations to generate common Getters, Setters,toString
etc.
import ; @Data public class Student { private String name; }
On the aboveStudent
In the category,@Data
The annotation comes from Lombok, which automatically generates all the Getters, Setters andtoString
etc. In this way, we don't need to manually write these common codes, making the code more concise.
2. Configuration class: Initialize Bean
Next, we need to create a configuration classTestConfig
, which defines a@Bean
Annotation method to initializeStudent
Object.
import ; import ; @Configuration public class TestConfig { @Bean public Student studentInit() { Student student = new Student(); ("initialization"); return student; } }
-
@Configuration
Annotation means that the class is a configuration class, and Spring scans the class and initializes the bean according to its bean definition. -
@Bean
Annotations are used to tell Spring containers:studentInit()
The object returned by the method (here isStudent
) should be managed as a bean. so,Student
The object will become a management object in the Spring container.
In this configuration class, we explicitly initialize aStudent
object, and set itname
The attribute is"initialization"
。
3. Test class:
use@Autowired
Automatic annotation injection bean
In the test class, we will pass@Autowired
The annotation willStudent
Objects are automatically injected and outputStudent
The name.
import ; import ; import ; @SpringBootTest public class StudentTest { @Autowired private Student student; @Test void contextLoads13() { (()); // Output: Initialization } }
-
@SpringBootTest
Annotation means that this is a Spring Boot test class that starts the Spring container for integration testing. -
@Autowired
Automatic annotation injectionStudent
Bean. Spring will automatically find the type that matches itStudent
Bean and inject into this field. - In the test method
contextLoads13()
In, call()
OutputStudent
The object'sname
Attribute value should be output"initialization"
, this is with usTestConfig
The values defined in the same way.
4. Automatic assembly of Spring Boot
- In this example, we see that
@Autowired
Annotation, Spring container willStudent
The type automatically injects us with the right bean. No manual configuration or instance creation is required. - This automatic injection mechanism is a very powerful feature in Spring Framework, which can greatly simplify the management of dependencies between classes.
5. Summary
Through the above examples, we learned the following points:
-
@Bean
Annotation: Through this annotation, we can explicitly define beans in the configuration class so that the object is managed by the Spring container. -
@Autowired
Annotation: Through this annotation, Spring will automatically inject beans into places where they need to depend on according to their type. -
@Data
Note: SimplifiedStudent
Class code does not require manual writing of Getter, Setter and other methods.
In actual development, Spring's dependency injection (DI) function greatly reduces the coupling between classes, improving the maintainability and scalability of the code. By using flexible@Autowired
and@Bean
Annotation can effectively manage and share objects.
This is the article about using `@Autowired` and `@Bean` annotations in Spring Boot. For more related springboot @Autowired and @Bean annotations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!