What annotation in the spring framework is to inject the class into the spring container

In the Spring framework, there are multiple annotations that can inject classes into the Spring container. The most commonly used annotations include the following:

@Component annotation: used to mark an ordinary Java class as a component that can be managed by the Spring container.

@Controller annotation: used to mark a class as a controller, usually used to handle user requests.

@Service annotation: Used to mark a class as a service class and used in the business layer.

@Repository annotation: Used to mark a class as a component of the data access layer, usually interacting with the database.

These annotations are scanning annotations in Spring, which are used to automatically scan and register the annotated class into the Spring container. After using any of these annotations, instances of the class will be created and managed by the Spring container.

In addition to the above commonly used annotations, there are other annotations to choose from, and you can also customize annotations to inject classes into the Spring container. It should be noted that in order for the Spring container to scan the annotated class, you need to specify the package path to scan in the configuration file or use the @ComponentScan annotation.

Guess you like

Origin blog.csdn.net/weixin_50503886/article/details/132010767