Conventional automatic assembly annotation @Autowired @RequiredArgsConstructor @AllArgsConstructor

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/fwk19840301/article/details/91430905

      "Spring combat" in to the assembly under a definition: create a collaborative relationship between the application object's behavior is called assembly. That is when an object's attributes is another object, an instance, it is necessary to instantiate the object properties. This is the assembly.

If the object is only to indicate a dependency through the interface, then this can be dependent in the case without the knowledge of the object itself, be embodied in different switching. But this will be a problem, configuration, we must be clear which give the property a bean reference assembly in the traditional dependency injection, once a lot of bean, bad maintained. Based on this scenario, spring uses annotations to perform automatic assembly, to solve this problem. Automated assembly is that developers do not have to know the specific references to the assembly which the bean, the identification work will be done by spring. Cooperating with the automatic assembly and "automatic detection", this action will automatically identify what needs to be configured to the bean class, and further to assemble. So we understand, automated assembly operation in order to inject dependency "automation" a simplified configuration.


Assembly divided into three: byName, byType, constructor.

  • byName is the name of the property will be the same bean assembly.
  • byType is the same as the bean property type assembly.
  • constructor is to be the same type as the parameter bean assembled by the constructor.

byType of @Autowired (spring offer), @ Inject (java ee provided)

@Autowired annotation is byType type, this annotation can be used in the above properties, the above aspects of the setter and the above builder. When using this annotation, you do not need to add setter methods for properties in the class. However, this attribute is mandatory, which means that you must get on the assembly, if appropriate, can be fitted on bean is not found, it will throw an exception. Required = false then you can use to allow the mounting may not be the default value is true. When required = true when, @ Autowired requirements must be assembled, but in the absence of the bean can be assembled, it will throw an exception: NoSuchBeanDefinitionException, if required = false, no exception is thrown.

  • @Inject must be mandatory assembly, there is no required attributes, that is, can not be null, if there is no matching bean, an exception is thrown.
  • Automatic assembly, assembly of bean attributes must be unique and consistent, no more and no less, and only one bean can be fitted to automatic assembly successful. Otherwise it will throw an exception.

byName of @Qualifier, @ Named (java ee provided) (spring offer), @ Resource (java ee provided)

There is more than one case at the same time is a type of bean, also throw this exception. At this point the need to further clarify the assembly which Bean, then you can use a combination of

@Qualifier annotation using byName assembled, so you can type in more of the same bean, bean clear which name to use for assembly. @Qualifier annotation played automatic assembly candidate narrowing effect of the bean.

@Autowired and @Qualifier spring is provided, @ Inject the @Named is the java ee.

constructor 的@Data  @RequiredArgsConstructor @AllArgsConstructor

@Component
@Slf4j
@AllArgsConstructor
class NoNeedInspectValidator {

    private final MaterialProxy materialProxy;
    private final ResultVOBuilder resultVOBuilder;
}
  • @Data
/**
 * Generates getters for all fields, a useful toString method, and hashCode and equals implementations that check
 * all non-transient fields. Will also generate setters for all non-final fields, as well as a constructor.
 * <p>
 * Equivalent to {@code @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode}.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/Data">the project lombok features page for &#64;Data</a>.
 * 
 * @see Getter
 * @see Setter
 * @see RequiredArgsConstructor
 * @see ToString
 * @see EqualsAndHashCode
 * @see lombok.Value
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
	/**
	 * If you specify a static constructor name, then the generated constructor will be private, and
	 * instead a static factory method is created that other classes can use to create instances.
	 * We suggest the name: "of", like so:
	 * 
	 * <pre>
	 *     public @Data(staticConstructor = "of") class Point { final int x, y; }
	 * </pre>
	 * 
	 * Default: No static constructor, instead the normal constructor is public.
	 * 
	 * @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
	 */
	String staticConstructor() default "";
}

According to the source you can see comment @Data contain @RequiredArgsConstructor

  • @RequiredArgsConstructor
/**
 * Generates a constructor with required arguments.
 * Required arguments are final fields and fields with constraints such as {@code @NonNull}.
 * <p>
 * Complete documentation is found at <a href="https://projectlombok.org/features/Constructor">the project lombok features page for &#64;Constructor</a>.
 * <p>
 * Even though it is not listed, this annotation also has the {@code onConstructor} parameter. See the full documentation for more details.
 * 
 * @see NoArgsConstructor
 * @see AllArgsConstructor
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface RequiredArgsConstructor {
	/**
	 * If set, the generated constructor will be private, and an additional static 'constructor'
	 * is generated with the same argument list that wraps the real constructor.
	 * 
	 * Such a static 'constructor' is primarily useful as it infers type arguments.
	 * 
	 * @return Name of static 'constructor' method to generate (blank = generate a normal constructor).
	 */
	String staticName() default "";
	
	/**
	 * Any annotations listed here are put on the generated constructor.
	 * The syntax for this feature depends on JDK version (nothing we can do about that; it's to work around javac bugs).<br>
	 * up to JDK7:<br>
	 *  {@code @RequiredArgsConstructor(onConstructor=@__({@AnnotationsGoHere}))}<br>
	 * from JDK8:<br>
	 *  {@code @RequiredArgsConstructor(onConstructor_={@AnnotationsGohere})} // note the underscore after {@code onConstructor}.
	 * 
	 * @return List of annotations to apply to the generated constructor.
	 */
	AnyAnnotation[] onConstructor() default {};
	
	/**
	 * Sets the access level of the constructor. By default, generated constructors are {@code public}.
	 * 
	 * @return The constructor will be generated with this access modifier.
	 */
	AccessLevel access() default lombok.AccessLevel.PUBLIC;
	
	/**
	  * Placeholder annotation to enable the placement of annotations on the generated code.
	  * @deprecated Don't use this annotation, ever - Read the documentation.
	  */
	@Deprecated
	@Retention(RetentionPolicy.SOURCE)
	@Target({})
	@interface AnyAnnotation {}
}

 The source can be seen Required arguments are final fields and fields with constraints such as {@code @NonNull}.

Comprising generates a constant, and a method of identifying a configuration of a variable NotNull. Generated constructor is private private

  •  @AllArgsConstructor

It generates a constructor contains all the variables

 


 

Guess you like

Origin blog.csdn.net/fwk19840301/article/details/91430905