[问题] 使用CDI注入物件到属性

楼主: lueichun (no anonymous)   2021-01-02 23:04:45
※状况概述:
我尝试用CDI,将DAO物件注入到位于某个Action的DAO属性里,不过都失败,看起来是
注入没有成功。
※程式码:
DAO:
@Named
@ApplicationScoped
public class TestDao {
......
}
Action:
@ManagedBean(name="testAction")
@SessionScoped
public class TestAction {
@Inject
TestDao testDao;
public List<Customer> query(){
customerList = testDao.query();//执行到这行出错
}
※错误讯息:
com.sun.faces.lifecycle.InvokeApplicationPhase execute
警告: #{testAction.query()}: java.lang.NullPointerException
javax.faces.FacesException: #{testAction.query()}:
java.lang.NullPointerException
threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
※补充说明:
看起来是注入没有成功才会跳NullPointerException,
DAO有用@Named跟@ApplicationScoped来注册物件跟指定存在的scope
action也有用@inject宣告要注入的属性
那请问要如何修改才能修正呢?

Links booklink

Contact Us: admin [ a t ] ucptt.com