user966506
user966506

Reputation: 142

Static Fields + Autowiring in Spring

Can we autowire static fields in spring controller ?

eg:

@Autowired
    public static JNDIEMailSender jNDIEmailSender;

Upvotes: 4

Views: 11292

Answers (2)

MooG
MooG

Reputation: 69

Spring doesnt autowire static fields

Upvotes: 0

gkamal
gkamal

Reputation: 21000

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
}

Upvotes: 10

Related Questions