@Autowired注解 required属性

在@Autowired注解中,有一个required属性,默认为true

@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.* 

Defaults to {@code true}.*/boolean required() default true;}

结论:

  • 当required属性为true的时候,注入SpringBean的时候,该bean必须存在,不然会注入失败,启动报错
  • 当required属性为false的时候,注入SpringBean的时候,如果bean存在,就注入;如果不存在,就忽略跳过,启动不会报错,但不能直接使用


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部