您可以将 @Autowired 与静态字段一起使用吗?
有没有办法将 @Autowired 与静态字段一起使用。 如果没有,还有其他方法可以做到这一点吗?
Is there some way to use @Autowired
with static fields. If not, are there some other ways to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
创建一个可以自动装配的 bean,它会初始化静态变量作为副作用。
Create a bean which you can autowire which will initialize the static variable as a side effect.
想要添加自动接线静态字段(或常量)将被忽略的答案,但也不会产生任何错误:
Wanted to add to answers that auto wiring static field (or constant) will be ignored, but also won't create any error:
您可以使用XML 表示法和
MethodInvokingFactoryBean
来实现此目的。 例如,请查看 这里。您应该尽可能使用 spring 注入,因为这是推荐的方法,但这并不总是可能的,因为我确信您可以想象,因为并非所有内容都可以从 spring 容器中提取,或者您可能会处理与遗留系统。
请注意,使用这种方法进行测试也可能会更加困难。
You can achieve this using XML notation and the
MethodInvokingFactoryBean
. For an example look here.You should aim to use spring injection where possible as this is the recommended approach but this is not always possible as I'm sure you can imagine as not everything can be pulled from the spring container or you maybe dealing with legacy systems.
Note testing can also be more difficult with this approach.
你可以使用ApplicationContextAware
然后
You can use ApplicationContextAware
then
免责声明 这绝不是标准的,很可能有更好的 Spring 方法来做到这一点。 上述答案都没有解决公共静态场的接线问题。
我想完成三件事。
我的对象看起来像这样
我们已经检查了 1 & 2 现在我们如何防止对 setter 的调用,因为我们无法隐藏它。
此方面将包装所有以 Final 开头的方法,并在调用它们时抛出错误。
我不认为这特别有用,但如果你有强迫症并且喜欢把豌豆和胡萝卜分开,这是一种安全的方法。
重要 Spring 在调用函数时不会调用您的方面。 让这变得更容易,糟糕的是我在弄清楚之前已经弄清楚了逻辑。
Disclaimer This is by no means standard and there could very well be a better spring way of doing this. None of the above answers address the issues of wiring a public static field.
I wanted to accomplish three things.
My object looks like this
We have checked off 1 & 2 already now how do we prevent calls to the setter, since we cannot hide it.
This aspect will wrap all methods beginning with final and throw an error if they are called.
I dont think this is particularly useful, but if you are ocd and like to keep you peas and carrots separated this is one way to do it safely.
Important Spring does not call your aspects when it calls a function. Made this easier, to bad I worked out the logic before figuring that out.
一般来说,通过对象实例设置静态字段是一种不好的做法。
为了避免可选问题,您可以添加
synchronized
定义,并仅在 private static Logger logger; 时设置它;:
Generally, setting static field by object instance is a bad practice.
to avoid optional issues you can add
synchronized
definition, and set it only if private static Logger logger;:
解决方案 1:使用构造函数
@Autowired
对于静态字段解决方案 2:使用
@PostConstruct
将值设置为静态字段请参阅此处了解更多详细信息
Solution 1 : Using Constructor
@Autowired
For Static FieldSolution 2 : Using
@PostConstruct
to set the value to Static FieldRefer here for more detail
我使用私有静态内部组件:FieldSetter,注入静态字段:MyBean,最后SelfDestroyBean将帮助我删除多余的FiledSetter< /strong> 豆
I use private static inner Component: FieldSetter, to inject static field: MyBean, at last SelfDestroyBean will help me remove redundant FiledSetter bean
简而言之,不。 您无法在 Spring 中自动装配或手动装配静态字段。 您必须编写自己的逻辑才能做到这一点。
In short, no. You cannot autowire or manually wire static fields in Spring. You'll have to write your own logic to do this.
@Autowired
可以与 setter 一起使用,这样您就可以让 setter 修改静态字段。最后一个建议...不要
@Autowired
can be used with setters so you could have a setter modifying an static field.Just one final suggestion... DON'T
在 @PostConstruct 方法中初始化自动装配的组件
Init your autowired component in @PostConstruct method