'Static Fields + Autowiring in Spring

Can we autowire static fields in spring controller ?

eg:

@Autowired
    public static JNDIEMailSender jNDIEmailSender;


Solution 1:[1]

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
}

Solution 2:[2]

Spring doesnt autowire static fields

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gkamal
Solution 2 MooG