Can you use @Autowired with static fields?

后端 未结 11 1379
臣服心动
臣服心动 2020-11-22 13:15

Is there some way to use @Autowired with static fields. If not, are there some other ways to do this?

11条回答
  •  遇见更好的自我
    2020-11-22 13:43

    Wanted to add to answers that auto wiring static field (or constant) will be ignored, but also won't create any error:

    @Autowired
    private static String staticField = "staticValue";
    

提交回复
热议问题