gpt4 book ai didi

java - CDI 注入(inject)的 bean 是否需要 getter 和 setter?

转载 作者:行者123 更新时间:2023-11-29 06:51:56 25 4
gpt4 key购买 nike

当使用 CDI 注入(inject) bean 时:

@Inject Person person;

是否需要/推荐 setter 和 getter?

import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class myJSFBean{

@Inject Person person;

public void setPerson (Person person){
this.person = person;
}
public Person getPerson (){
return person;
}

我找到了这个文档,但我不明白它的意思: http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html

最佳答案

根据 Weld Documentation :

Notice that it isn’t necessary to create a getter or setter method to inject one bean into another. CDI can access an injected field directly (even if it’s private!), which sometimes helps eliminate some wasteful code. The name of the field is arbitrary. It’s the field’s type that determines what is injected.

关于java - CDI 注入(inject)的 bean 是否需要 getter 和 setter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45078003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com