@AgentBean @Component类中的@Autowired服务在JSF请求期间为空 [英] @Autowired service inside a @ManagedBean @Component class is null during JSF request

查看:218
本文介绍了@AgentBean @Component类中的@Autowired服务在JSF请求期间为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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());
}
}

  @ManagedBean(name =studentBean)
@Scope
@Component
public class StudentBean {

@Autowired
private TestService testService;

public void printString(){
System.out.println(testService.getString());
}
}

对于这些文件,我有正确的配置spring,jsf和web.xml。并有.xhtml页面,在那里我启动printString()为'someBean'和'StudentBean'。我在第一种情况下有NPE,在第二种情况下,在控制台中有一些字符串。
原因很简单 - 在Spring上下文和JSF中有不同的bean名称。所有问题在

  @Component => @Component(userBean)
public class someBean {

在调试中我看到

  private TestService testService; 

@Autowired
public void setTestService(TestService testservice){
this.testService = testService;
}

当JSF bean正在创建testService集时不为null,但在JSF中为空生活周期当

  public void pringString(){
testService.blah();
}

testService为null。这是我不明白的。有人深入了解Spring和JSF来详细描述这种情况吗?

解决方案

JSF和Spring都可以作为bean容器。 @ManagedBean 注释指示JSF托管bean工具创建该类的新实例,并以给定的名称进行管理。 @Component 注释指示Spring ApplicationContext创建类的新实例,并以给定的名称进行管理。也就是说,JSF和Spring都创建了一个类的实例,JSF可以通过EL访问,但是Spring可以注入它的依赖项(因为,作为弹簧注释,@Autowired不被JSF托管bean设备所理解)



所以你有一个选择:使用JSF托管bean设施进行一切(我不会推荐,因为它是有限的),使用CDI的一切是一个选项,但不使用Spring),或者通过删除 @ManagedBean 注释的所有东西(通常使用Spring),并使Spring bean可以通过EL访问通过在faces-config.xml中注册一个 SpringBeanFacesELResolver 。 Spring参考手册在第19.3.1节


I tried union Spring 3(MVC) with JSF 2. I have some experience in Spring and JSF, but never tried to join them before. In the end I have 2 files

@ManagedBean(name = "userBean")
@Scope
@Component
public class someBean {

  @Autowired
  private TestService testService;

  public void printString() {
    System.out.println(testService.getString());
  }
}

and

@ManagedBean(name = "studentBean")
@Scope
@Component
public class StudentBean {

  @Autowired
  private TestService testService;

  public void printString() {
    System.out.println(testService.getString());
  }
}

For these file I have right configuration for spring, jsf, and web.xml. And have .xhtml page where I start printString() for 'someBean' and for 'StudentBean'. I have the NPE in first case and 'some string' in the console in second case. The reason is simple - different bean names in the Spring context and JSF. all problems finished after

@Component => @Component("userBean") 
public class someBean {

In the debug I saw that

private TestService testService;

@Autowired
public void setTestService(TestService testservice) {
  this.testService = testService;
}

When JSF bean is creating testService sets not null, but it is null during JSF lifecycle when

public void pringString() {
  testService.blah();
}

testService is null. It is what I can't understand. Has someone deep knowledge the Spring and JSF to describe this situation in details?

解决方案

Both JSF and Spring can act as bean containers. The @ManagedBean annotation instructs the JSF managed bean facility to create a new instance of the class, and manage it under the given name. The @Component annotation instructs the Spring ApplicationContext to create a new instance of the class, and manage it under the given name. That is, both JSF and Spring create an instance of that class, the JSF one is reachable through EL, but the Spring one gets its dependencies injected (because, being a spring annotation, @Autowired is not understood by the JSF managed bean facility).

So you have a choice: Use the JSF managed bean facility for everything (which I would not recommend, as it is rather limited), use CDI for everything (which is an option, but does not use Spring), or use Spring for everything (which I usually do), by removing the @ManagedBean annotation, and making Spring beans accessible through EL by registering a SpringBeanFacesELResolver in your faces-config.xml. The Spring reference manual describes this in section 19.3.1.

这篇关于@AgentBean @Component类中的@Autowired服务在JSF请求期间为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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