弹簧中的静态场+自动装配 [英] Static Fields + Autowiring in Spring

查看:75
本文介绍了弹簧中的静态场+自动装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在Spring控制器中自动为静态场接线吗?

Can we autowire static fields in spring controller ?

例如:

@Autowired
    public static JNDIEMailSender jNDIEmailSender;

推荐答案

不,我认为这不起作用.您可以添加一个setter方法,使用@Autowired对其进行注释,并在setter中设置静态字段.

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
}

这篇关于弹簧中的静态场+自动装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆