0

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

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

这个类被命名AvisoRecaladaBean,它有 3 个用@ManagedProperty注解定义的属性,如下所示:

@ManagedProperty("#{jsf2Util}")
private Jsf2Util jsf2Util;

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

@ManagedProperty("#{usuarioService}")
private IUsuarioService usuarioService;

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

另外,我定义了一个名为的Integer属性folioBusqueda和一个List<SigcueCertAvisoRecalada>名为的属性listadoSigcueCertAvisoRecalada是一个实体类,指向一开始提到的数据库中的一个表。

上面提到的每个属性都有它的 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>

里面的actionListener命令按钮是指下面的方法AvisoRecaladaBean

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属性为空,而其他两个托管属性却不是

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

  1. 添加和/或替换@ManagedProperty@Autowired
  2. 使用@Qualifier注解来命名 bean(我命名它"avisoRecaladaService")并使用 currentApplicationContext来获取 bean(来源:Spring Bean never set as ManagedProperty in JSF Bean)。我得到了NoSuchBeanException这个解决方案:
WebApplicationContext webAppContext = ContextLoader.getCurrentWebApplicationContext();
avisoRecaladaService = (IAvisoRecaladaService) webAppContext.getBean("avisoRecaladaService");
  1. 编辑:直接实例化avisoRecaladaService。不可取。此外, SigcueCertAvisoRecaladaService 的自动装配属性为 null
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. 编辑:替换@ManagedAttribute@Resource(来源:Spring 中的 @ManagedProperty 等效项
  2. 替换@ManagedAttribute@Inject(与之前的解决方案相同的来源)

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

编辑

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

  • jboss-jsf-api 2.2
  • jboss-el-api 3.0 规范
  • 弹簧芯 4.2.8
  • 弹簧网 4.2.8
  • 弹簧表达式 4.2.8
  • 弹簧上下文支持 4.2.8
  • spring-web-mvc 4.2.8
  • JDK 1.8.0_191
  • 在 Eclipse Oxygen 中开发(如果相关)
4

1 回答 1

0

解决了

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

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

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

进入这个:

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

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

于 2020-06-19T17:42:26.690 回答