Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Fields + Autowiring in Spring

Can we autowire static fields in spring controller ?

eg:

@Autowired
    public static JNDIEMailSender jNDIEmailSender;
like image 557
user966506 Avatar asked Dec 27 '22 13:12

user966506


1 Answers

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
}
like image 117
gkamal Avatar answered Dec 31 '22 11:12

gkamal