@Resource annotation liberates the difference between attributes and methods

In the spring project, automatic assembly can use @Autowiredannotations or the @Resourceannotations in the JSR-250 specification. Take @Resourceannotations here . The annotations can be placed on the attributes or methods of the class (of course, it can also be placed on the class). In the two cases, the effect is the same, which one can be used according to the company's specifications, but there are still some differences.

  • Add on the attributes of the class
  1. First use the field name to match the bean. If the bean is found, it will be injected. If the type does not match, there will be an exception at this time and the injection will fail.
  2. If the field name does not match the Bean, Spring will try to match the field type. If it finds a bean, it will inject. If the field type is an interface, it may match multiple types, and an exception that matches multiple beans will be thrown. Injection failed.
  • On the setter method
  1. First use the attribute name to match the bean. If the bean is found, it will be injected. If the type does not match, there will be an exception at this time and the injection will fail.
  2. If the attribute name does not match the Bean, Spring will try to match the attribute parameter type. If the bean is found, it will be injected. If the attribute parameter type is an interface, it may match multiple types, and it will throw out matches to multiple beans. Exception. Injection failed. 
    注意是属性的参数类型,并不是属性的类型,注意区别
  • Netizen replied:

Let's put it this way, if the annotation is written on the attribute, then the underlying operation is: find that the attribute has the annotation, then get the name of the annotation, find the entity corresponding to the name, and set the value according to the type if there is no name. Then find the setter method corresponding to the property, and then call the setter to pass in the corresponding value.
If you write directly to the setter method, then it is the same operation, but the difference is that writing on the property is one more step to find the operation of the setter method, and writing on the setter is missing this step, which is an optimization item.
In terms of performance, the consumption is not very large. If it is not a service with a huge amount of operation, it is recommended to write on the field for general applications and services, because it is easy to maintain and view, because it has little impact on performance.
If it is a system with a huge number of operations, such as Taobao, it is strongly recommended to write it on the setter. Specifically, you can write the demo yourself and execute it in a loop 100 million times, or 1 billion times, and you can see a more obvious difference, but You will also find that 100 million operations are actually not much difference

You can refer to the advantages and disadvantages of the three methods of Spring annotation dependency injection

Guess you like

Origin blog.csdn.net/qq_40084325/article/details/111386064