Detailed explanation of spring dependency injection: @ Autowired, @ resource and @ inject differences and implementation principles

Time:2019-11-8

I. usage of spring dependency injection

  • @Autowired is the annotation provided by spring framework to implement dependency injection. It mainly supports bean injection in set method, field and constructor. The injection method is to find beans by type, that is bytype. If there are multiple beans of the same type, use @ qualifier to specify which bean name to inject.
  • Difference from @ resource of JDK: @ resource is based on bean name, that is, bean name, to find bean injection from spring IOC container, while @ autowritten is based on type bytype.
  • Difference from @ inject of JDK: @ inject is also based on type to find bean injection. If you need to specify the name beanname, you can use the @ named annotation in combination with @ Autowired, and you can use the @ qualifier annotation to specify the name beanname.

2. Implementation principle of spring dependency injection annotation

annotation processor

  • In the internal implementation of spring framework, annotation implementation injection is mainly implemented by the implementation class of bean postprocessor interface. Beanpostprocessor postprocessor is executed immediately after creating bean object instance when spring container is started to process bean object instance.
  • @Autowired implements dependency injection of bean objects to other bean objects through the implementation class of the beanpostprocessor interface, Autowired annotation beanpostprocessor;
  • @Resource and @ inject are implemented through the implementation class commonannotationbeanpostprocessor of the beanpostprocessor interface. As the name indicates, commonannotation is a beanpostprocessor that uniformly processes annotations defined in JDK in spring. The annotations processed by this class also include @ postconstruct, @ predestroy, etc.

Note processor activation conditions

  • The conditions for Autowired annotation beanpostprocessor and commonannotation beanpostprocessor to be added to the beanpostprocessor of spring container are as follows:

1. Spring configuration based on XML

  • In the configuration XML file of the corresponding spring container, such as applicationcontext.xml, add < context: annotation config / > and < context: component scan / > or use only < context: component scan / >.
  • The difference between the two is that < context: annotation config / > only finds and activates the existing beans, such as those generated and loaded into the spring container through the bean tag of the XML file, instead of scanning the annotated beans such as @ controller and injecting them after finding them; while < context: component scan / > in addition to its < context: annotation config / > function, it also loads the beans through the basepack Under the package specified by the ages attribute, the default is to scan @ controller, @ service, @ component, @ repository annotation classes. If basepackages is not specified, it is under the class path, or if the annotation @ componentscan method is used, it is under the package and its subpackages where the current class is located.

2. Spring configuration based on configuration class

  • If spring is configured based on configuration classes rather than applicationcontext.xml, such as springboot, the IOC container used internally is implemented as annotationconfidapplicationcontext or its derived class, and the above beanpostprocessor will be created and activated automatically within annotationconfidapplicationcontext.
  • If there is both XML based configuration and configuration class configuration, and in terms of injection time, annotation based injection is prior to XML based injection, so XML based injection will cover annotation based injection.

summary

  • @Autowired comes with spring, @ inject and @ resource are provided by JDK, where @ inject is implemented by jsr330 specification, @ resource is implemented by jsr250 specification, and spring provides support for JDK specification through beanpostprocessor.
  • @The usage of Autowired and @ inject are basically the same. The difference is that @ Autowired has a required attribute, which indicates whether the injection is necessary. That is to say, if it is necessary, if the corresponding bean cannot be found, it cannot be injected and the current bean cannot be created.
  • @Autowired, @ inject are matched by type by default, @ resource is matched by name. For example, the redistemplate bean generated automatically in the spring boot data project needs to be injected through byname. If you need to inject the default, you need to use @ resource instead of @ Autowired.
  • For @ autowire and @ inject, if there are multiple bean instances of the same type, you need to specify the injected bean name. @Autowired is used with @ qualifier, @ inject and @ name.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

The use of progressbarcontrol, a progress bar control of devexpress – Taking ZedGraph as an example to add curve progress

scene WinForm control – devexpress18 download installation registration and use in vs: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 When using ZedGraph to add curves, the number of curves is slower if there are many cases. So in the process of adding curve, the progress needs to be displayed, and the effect is as follows     Note: Blog home page:https://blog.csdn.net/badao_liumang_qizhi […]