是否可以将CDI Bean注入Java EE 6中的静态变量? [英] Is it possible to inject a CDI Bean into a static variable in Java EE 6?

查看:87
本文介绍了是否可以将CDI Bean注入Java EE 6中的静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能吗:

@Inject
@MessageTransport(MessageTransportType.SMS)
public static MessageSender messageSender;

当我试图访问这个静态变量时,我得到了一个N​​PE。所以我想知道,如果一般情况下不可能的话。

I'm getting a NPE when i'm trying to access this static variable. So I wonder, if it is not possible in general.

提前致谢。

推荐答案

一般情况下没做,因为静态变量不能有一个范围,即它只是整个类的一个(读取应用程序),因此它没有意义,因为每个实例都会尝试将它设置为一个新的基于当前范围的价值。

Not done in general because a static variable cannot have a scope, i.e. it's just one for the whole class (read application) and therefore it doesn't make sense since every instance would try to set it to a new value based on the current scope.

这篇关于是否可以将CDI Bean注入Java EE 6中的静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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