summary

  Spring's container management of beans is divided into two processes

  1. Instantiate beans

  2. Complete the injection through various dependencies of the bean

  

  instantiation of beans

  For the instantiation of beans, spring can automatically scan the beans that need to be instantiated through annotations. After adding the annotations to the class through the @omponent annotation, spring will instantiate the class and put it into the container for unified management. Implicit methods cannot be initialized for third-party libraries.

  Beans can also be instantiated by explicit configuration. There are two cases for this:

  1. Through java code. Write a separate javaconfig class in the code and add the @configtion annotation to the class. The key to creating a JavaConfig class is to add the @Configuration annotation to it. New instance objects are added to the method in the class, and the @Bean annotation is added to the method. .

  2. Configured through the XML file, this will not go into detail.

Above are the three ways to instantiate a bean.

  Inject Bean dependencies

  Injection is usually done using the @Autowire annotation. There are other objects in the class that need to be imported

 

  Turn on scan

  Turning on scanning is mainly for implicitly annotated @copmontent, and only implicit annotations need to be scanned.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325067314&siteId=291194637