弹簧定义@ManagedProperty的问题



我目前正在维护一个基于Maven的JSF Web应用程序,该应用程序与Spring Framework和JPA相结合,并连接到SQL Server数据库。

在应用程序中,我创建了一个使用@ViewScoped@Scope("view")注释定义的@ManagedBean类。

此类名为AvisoRecaladaBean,它有 3 个使用@ManagedProperty注释定义的属性,如下所示:

@ManagedProperty("#{jsf2Util}")
private Jsf2Util jsf2Util;
@ManagedProperty("#{avisoRecaladaService}")
private ISigcueCertAvisoRecaladaService avisoRecaladaService;
@ManagedProperty("#{usuarioService}")
private IUsuarioService usuarioService;

第一个和第三个属性用于同一应用程序中的其他受管 Bean。此外,IUsuarioServiceISigcueAvisoRecaladaService是接口,每个接口都由一个使用@Service注释定义的类实现。实现后一个接口的类也具有@Transactional注释。JsfUtil是一个也用@Service定义的类。

此外,我定义了一个名为folioBusquedaInteger属性和一个名为listadoList<SigcueCertAvisoRecalada>属性。SigcueCertAvisoRecalada是一个实体类,指向一开始就显示的数据库中的表。

上面提到的每个属性都有它的getter和setter。

另一方面,我创建了一个名为avisoRecalada.xhtml的XHTML页面,它适用于AvisoRecaladaBean管理的Bean。

XHTML页面有一个面板网格,定义如下:

<h:panelGrid columns="3">
<label>Ingrese Número de Folio: *</label>
<p:inputNumber placeholder="Folio del Aviso Recalada"
value="#{avisoRecaladaBean.folioBusqueda}"
required="true"
id="numeroFolio"/>
<p:commandButton value="Obtener Certificado Aviso" 
actionListener="#{avisoRecaladaBean.buscarRegistro()}" 
update="idTablaAviso"/>
<h:message for="numeroFolio" style="color:red"/>
</h:panelGrid>

命令按钮内的actionListenerAvisoRecaladaBean中引用以下方法

public void buscarRegistro() {
SigcueCertAvisoRecalada item = avisoRecaladaService.findByFolio(folioBusqueda);
listado.clear();
if(item!=null) {
listado.add(item);
}
}

Spring 配置在 XML 文件中定义,定义如下(我只显示重要部分(:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:drools="http://drools.org/schema/drools-spring"   
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd       
">      
<context:component-scan base-package="cl.sernapesca" />
<context:annotation-config />
<!-- Bean definitions -->
<tx:annotation-driven/> 
<tx:jta-transaction-manager />  
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:singleSingOn.properties</value>
<value>classpath:revision.properties</value>
<value>classpath:ldapExternos.properties</value> 
</list>
</property>
</bean>  
<!-- View Scope para JSF2 -->
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="cl.sernapesca.mantenedorcentral.arquitectura.ViewScope" />
</entry>
</map>
</property>
</bean>
<!-- More Bean definitions -->
</beans>

faces-config.xml 只定义了以下受管 Bean:

<managed-bean>
<managed-bean-name>currentDate</managed-bean-name>
<managed-bean-class>java.util.Date</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

它的解析器定义为:

<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<!-- More configurations -->
</application>

当我使用 WildFly 10 应用程序服务器部署应用程序时,没有收到任何错误消息。此外,当我访问 XHTML 页面时,我没有收到任何错误。

但是,当我在输入文本中输入一个值并按下命令按钮时,我得到一个NullPointerException.堆栈跟踪指示在尝试执行第一行buscarRegistro()时启动了异常。

经过一些调试,我发现avisoRecaladaService属性为 null,但其他两个托管属性不是

我尝试了以下解决方案,但无济于事

  1. 添加和/或将@ManagedProperty替换为@Autowired
  2. 使用@Qualifier注解来命名 bean(我将其命名为"avisoRecaladaService"(,并使用当前ApplicationContext来获取 bean(来源:Spring Bean 从未在 JSF Bean 中设置为 ManagedProperty(。我对这个解决方案有一个NoSuchBeanException
WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
avisoRecaladaService = (IAvisoRecaladaService) webAppContext.getBean("avisoRecaladaService");
  1. EDIT:直接实例化 avisoRecaladaService。不可取。此外,SigcueCertAvisoRecaladaService 的自动连线属性为空
public void buscarRegistro() {
if(avisoRecaladaService==null)
avisoRecaladaService=new SigcueCertAvisoRecaladaService();
SigcueCertAvisoRecalada item = avisoRecaladaService.findByFolio(folioBusqueda);
listado.clear();
if(item!=null) {
if(listado==null)
listado=new ArrayList<>();
listado.add(item);
}
}
  1. EDIT:将@ManagedAttribute替换为@Resource(来源:春季@ManagedProperty等效
  2. (
  3. @ManagedAttribute替换为@Inject(与以前的解决方案相同的源(

任何关于最终解决方案的建议都将是非常有义务的。

编辑

根据Kukeltje 的要求,根据应用程序的 pom.xml,涉及的库如下:

  • JBos-JSF-API 2.2
  • JBos-el-API 3.0 规范
  • 弹簧芯 4.2.8
  • 弹簧网 4.2.8
  • 弹簧表达式 4.2.8
  • 弹簧上下文支持 4.2.8
  • 弹簧网-MVC 4.2.8
  • JDK 1.8.0_191
  • 在Eclipse Oxygen中开发(如果相关(

已解决

感谢我的一个合作伙伴,我们发现@ManagedProperty对名字非常敏感。Bean 的名称必须与类完全相同,其首字母为小写。如果对象是一个接口实例,就像我的情况一样,它必须是实现它的类的名称。

所以,我不得不改变这个:

@ManagedProperty("#{avisoRecaladaService}")
private ISigcueCertAvisoRecaladaService avisoRecaladaService;

进入这个:

@ManagedProperty("#{sigcueCertAvisoRecaladaService}")
private ISigcueCertAvisoRecaladaService sigcueCertAvisoRecaladaService;

感谢大家阅读本文,希望这有助于将来出现类似的问题

最新更新