什么使一个bean是一个CDI bean? [英] What makes a bean a CDI bean?
问题描述
在这个问题的最高答案中,例如: Java EE 6 @ javax.annotation.ManagedBean vs. @ javax.inject.Named vs. @ javax.faces.ManagedBean 我读到:
In the top answer to this question for example : Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean I read that:
要部署CDI bean,必须在类路径上的
META-INF文件夹中放置一个名为beans.xml的文件。一旦这样做,那么
中的每个bean都将成为一个CDI bean。
To deploy CDI beans, you must place a file called beans.xml in a META-INF folder on the classpath. Once you do this, then every bean in the package becomes a CDI bean.
还有人说:
如果要使用JSF页面中的CDI bean,可以使用javax.inject给它一个
名称。
If you want to use the CDI bean from a JSF page, you can give it a name using the javax.inject.Named annotation.
我有一个示例代码,如下所示:
I have a sample code that goes like this:
@ManagedBean
@ViewScoped
public class SignUpPage {
private User user;
@PostConstruct
public void init() {
user = new User();
}
@Inject
private UserDao userDao;
// rest of the class
据我所知,我的bean仍然是一个JSF托管Bean,它不是一个CDI bean(或者是吗?)。顺便说一下,我的WEB-INF文件夹中有一个beans.xml。
So as far as I understand, my bean is still a JSF Managed Bean, it is not a CDI bean(or is it?). By the way, I have a beans.xml in my WEB-INF folder.
而@Inject在这里工作很好。此外,我可以使用EL访问bean(这让我认为它仍然是一个JSF Managed Bean)
And @Inject works just fine in here. Also, I can access the bean with EL just fine(which makes me think it is still a JSF Managed Bean)
UserDao类看起来像这样:
The UserDao class looks something like this:
@Stateless
public class UserDao {
EntityManager em;
@PostConstruct
public void initialize(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Persistence");
em = emf.createEntityManager();
}
所以,这是我知道的一个EJB ..
So, it is as far as I know an EJB..
这个例子中是否有任何CDI bean? @Inject如何在这里工作?
So do I have any CDI beans in this example? How does @Inject work here?
希望我的问题很清楚,请问!
Hope my question is clear, Regards!
推荐答案
规范,每个JavaBean都是一个托管Bean
(不要混淆JSF @ManagedBean
,这是一个不同的)在项目中,$ code> beans .xml 存在。所以每个类也有资格进行依赖注入。请注意,此类的默认范围是从属
。
By CDI specification, every JavaBean is a Managed Bean
(do not confuse it with JSF @ManagedBean
, this is a different one) in project where the beans.xml
is present. So every class is also eligible for dependency injection. Note that default scope of this class is Dependent
.
这篇关于什么使一个bean是一个CDI bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!