Static Fields + Autowiring in Spring

前端 未结 2 1161
不思量自难忘°
不思量自难忘° 2021-01-13 13:03

Can we autowire static fields in spring controller ?

eg:

@Autowired
    public static JNDIEMailSender jNDIEmailSender;
相关标签:
2条回答
  • 2021-01-13 13:23

    Spring doesnt autowire static fields

    0 讨论(0)
  • 2021-01-13 13:24

    No, I don't think that will work. You can add a setter method, annotate it with @Autowired and set the static field in the setter.

    @Autowired
    void setJNDIEmailSender(JNDIEmailSender jndiEmailSender) {
      ClassName.jNDIEmailSender = jndiEmailSender
    }
    
    0 讨论(0)
提交回复
热议问题