我尝试使用JSF 2进行联合Spring 3(MVC).我在Spring和JSF中有一些经验,但之前从未尝试过加入它们.最后我有2个文件
@ManagedBean(name = "userBean")
@Scope
@Component
public class someBean {
@Autowired
private TestService testService;
public void printString() {
System.out.println(testService.getString());
}
}
Run Code Online (Sandbox Code Playgroud)
和
@ManagedBean(name = "studentBean")
@Scope
@Component
public class StudentBean {
@Autowired
private TestService testService;
public void printString() {
System.out.println(testService.getString());
}
}
Run Code Online (Sandbox Code Playgroud)
对于这些文件,我有正确的spring,jsf和web.xml配置.并有.xhtml页面,我为'someBean'和'StudentBean'启动printString().我在第一种情况下使用NPE,在第二种情况下在控制台中使用"some string".原因很简单 - Spring上下文和JSF中的bean名称不同.所有问题都完成了
@Component => @Component("userBean")
public class someBean {
Run Code Online (Sandbox Code Playgroud)
在调试中我看到了
private TestService testService;
@Autowired
public void setTestService(TestService testservice) {
this.testService = testService;
}
Run Code Online (Sandbox Code Playgroud)
当JSF bean创建的testService集不为null时,但在JSF生命周期中它为null时
public void pringString() {
testService.blah();
}
Run Code Online (Sandbox Code Playgroud)
testService为null.这是我无法理解的.有谁深入了解Spring和JSF详细描述这种情况?