Common and different points of @Resource and @Autowired

table of Contents

Common points:

@Resource and @Autowired are both used for bean injection

Both @Resource and @Autowired can be marked on fields and methods


difference:

@Autowired is the annotation provided by Spring, and the imported dependency is org.springframework.beans.factory.annotation.Autowired

@Resource is provided by J2EE, and the imported dependency is javax.annotation.Resource

The @Autowired annotation defaults to assemble dependent objects according to the type (byType). If there is a bean of the same type as the specified attribute in the spring container, it will be automatically assembled with the attribute. If there are multiple beans of this type, an exception will be thrown. By default, @Autowired requires that the dependent object must exist, otherwise an exception will be thrown. Of course, we can also set the required attribute of the @Autowired annotation (this is also the only attribute of the Autowired annotation) and set required to false.

@Autowired source code is as follows:

@Target({
    
    ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
    
    

	/**
	 * Declares whether the annotated dependency is required.
	 * <p>Defaults to {@code true}.
	 */
	boolean required() default true;

}

@Resource defaults to automatic assembly by name (byName). It will find beans with the same bean name as the @Resource attribute name in the spring container and automatically assemble them to the attributes marked by @Resource. In addition to byName configuration, @Resource also supports byType configuration. You only need to configure the type attribute of @Resource annotation.

The following describes the assembly sequence of @Resource:

① If both name and type are specified, the only matching bean will be found from the Spring context for assembly, and an exception will be thrown if not found.

② If the name is specified, the bean matching the name (id) is searched from the context for assembly, and an exception is thrown if it cannot be found.

③ If the type is specified, the only similar matching bean is found from the context for assembly. If it is not found or if more than one is found, an exception will be thrown.

④ If neither the name nor the type is specified, it will automatically be assembled in the byName method; if there is no match, it will fall back to an original type for matching, and if it matches, it will be automatically assembled.

The source code of @Resource is as follows:

@Target({
    
    TYPE, FIELD, METHOD})
@Retention(RUNTIME)
@Repeatable(Resources.class)
public @interface Resource {
    
    
    /**
     * The JNDI name of the resource.  For field annotations,
     * the default is the field name.  For method annotations,
     * the default is the JavaBeans property name corresponding
     * to the method.  For class annotations, there is no default
     * and this must be specified.
     */
    String name() default "";

    /**
     * The Java type of the resource.  For field annotations,
     * the default is the type of the field.  For method annotations,
     * the default is the type of the JavaBeans property.
     * For class annotations, there is no default and this must be
     * specified.
     */
    Class<?> type() default java.lang.Object.class;

    ....省略其他属性字段
}

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/110767916