Spring服务不是通过@ManagedProperty注入的

时间:2016-03-20 19:16:08

标签: spring jsf jsf-2 dependency-injection

请你帮我解决这个例外吗?我查看了论坛,但我找不到任何解决方案。 我找不到nullPointer的原因! 我正在使用spring,hibernate和jsf。我既不能将bean插入数据库也不能获取行并将它们打印到dataTable中。 错误:

shade = ...

faces-config.xml:

Avertissement:   #{beanMateriel.insert()}: java.lang.NullPointerException
javax.faces.FacesException: #{beanMateriel.insert()}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 35 more
Caused by: java.lang.NullPointerException
    at jsf.bean.BeanMateriel.insert(BeanMateriel.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:332)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:283)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 36 more

Grave:   javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at jsf.bean.BeanMateriel.insert(BeanMateriel.java:108)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:332)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:283)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 36 more

applicationContext.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
    <managed-bean>
        <managed-bean-name>beanMateriel</managed-bean-name>
        <managed-bean-class>jsf.bean.BeanMateriel</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>materielBO</property-name>
            <value>#{materielBO}</value>
        </managed-property>
    </managed-bean>
</faces-config>

web.xml:

    <?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"></property>
<property name="url" value="jdbc:derby://localhost:1527/InterventionsDB;create=true"></property>
<property name="username" value="omar"/>
<property name="password" value="omar"/>
</bean>
<aop:config proxy-target-class='true'/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref = "dataSource"/>
    <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="connection.autoReconnect">true</prop>
            <prop key="connection.autoReconnectForPools">true</prop>
            <prop key="connection.is-connection-validation-required">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id ="materielDAO" class = "dao.ImplMateriel">
    <property name ="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id ="materielBO" class = "bo.MaterielImplBO">
    <property name ="materielDAO" ref="materielDAO"></property>
</bean>

</beans>

materiel.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           classpath*:/WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/login.xhtml</welcome-file>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

BeanMateriel.java:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"  
xmlns:p="http://primefaces.org/ui">

    <h:head>
        <title>Fiche Materiel</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </h:head>
    <h:body>
<h:form>
    <h1 style="FONT-SIZE: xx-large; color:#8080C0;font-family:Times New Roman;"></h1>
    <h:panelGrid>
    <table>
        <tr>
            <TD>
                <p:outputLabel for="codem" value="Code Matériel" />
            </TD>
            <td>
             <p:inputText id="codem" value="#{beanMateriel.codem}" label="CodeM" required="true"></p:inputText>
            </td>
        </tr>
        <tr><td> <p:outputLabel for="libelle" value="Libellé" /></td>
            <td><p:inputText id="libelle" value="#{beanMateriel.libelle}" required="true" label="Libelle"></p:inputText></td></tr>
        <tr><td> <p:outputLabel for="datemiseenservice" value="Date Mise en Service" /></td>
            <td>  <p:calendar id="datemiseenservice" value="#{calendarView.date2}" required="true" label="datemiseenservice"/></td></tr>
        <tr><td> <p:outputLabel for="marque" value="Marque" /></td>
        <td><p:inputText id="marque" value="#{beanMateriel.marque}" required="true" label="marque"></p:inputText></td></tr>
        <tr><td> <p:outputLabel for="departement" value="Département" /></td>
            <td> <p:inputText id="departement" value="#{beanMateriel.departement}" required="true" label="departement"></p:inputText></td></tr>
        <tr>
            <td></td>
            <td><p:commandButton value="Valider" type="submit" icon="ui-icon-check" action="#{beanMateriel.insert()}" readonly="false"/></td>
        </tr> 
        </table>
        </h:panelGrid>

    <p:dataTable id="table" value="#{beanMateriel.liste}" var="obj">
        <p:column headerText="Code">

                  <h:outputText value="#{obj.codem}"></h:outputText>
              </p:column>
              <p:column headerText="Libelle">
              <h:outputText value="#{obj.libelle}"></h:outputText>
              </p:column>
              <p:column headerText="Marque">

                  <h:outputText value="#{obj.marque}"></h:outputText>
              </p:column>
            <p:column headerText="Date mise en service">

                <h:outputText value="#{obj.datemiseenservice}"></h:outputText>
              </p:column>
              <p:column headerText="Département">

                  <h:outputText value="#{obj.departement}"></h:outputText>
              </p:column>

     <p:column headerText="modifier">    
         <p:commandButton icon="ui-icon-star" title="Icon Only" action="#{beanMateriel.update()}" immediate="true">

</p:commandButton>

    </p:column>
        <p:column headerText="supprimer">    
                  <p:commandButton icon="ui-icon-star" title="Icon Only" action="#{beanMateriel.delete()}" immediate="true">

</p:commandButton>
 </p:column>    
          </p:dataTable>
</h:form>
    </h:body>
</html>

ImplMateriel.java:

@ManagedBean(name="beanMateriel")

public class BeanMateriel implements Serializable{
     private int codem;
     private String libelle;
     private String marque;
     private String datemiseenservice;
     private String departement;
      private List<BeanMateriel> liste; 
     @ManagedProperty(value = "#{materielBO}")
     private MaterielImplBO materielBO; 


    public BeanMateriel() {
       }

    /**
     * @return the materielBO
     */
    public MaterielImplBO getMaterielBO() {
        return materielBO;
    }

    /**
     * @param materielBO the materielBO to set
     */
    public void setMaterielBO(MaterielImplBO materielBO) {
        this.materielBO = materielBO;
    }

    public List<BeanMateriel> getListe() {
        return liste;
    }

    public void setListe(List<BeanMateriel> liste) {
        this.liste = liste;
    }

    public BeanMateriel(int codem, String libelle, String marque, String datemiseenservice, String departement, MaterielImplBO materielBO, List<BeanMateriel> liste) {
        this.codem = codem;
        this.libelle = libelle;
        this.marque = marque;
        this.datemiseenservice = datemiseenservice;
        this.departement = departement;
        this.materielBO = materielBO;
        this.liste = liste;
    }



    public int getCodem() {
        return codem;
    }

    public void setCodem(int codem) {
        this.codem = codem;
    }

    public String getLibelle() {
        return libelle;
    }

    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public String getMarque() {
        return marque;
    }

    public void setMarque(String marque) {
        this.marque = marque;
    }

    public String getDatemiseenservice() {
        return datemiseenservice;
    }

    public void setDatemiseenservice(String datemiseenservice) {
        this.datemiseenservice = datemiseenservice;
    }

    public String getDepartement() {
        return departement;
    }

    public void setDepartement(String departement) {
        this.departement = departement;
    }

    public String insert() {

        materielBO.insert(this);
        getAll();
        return "";
    }

     public String delete() {
         materielBO.delete(this);
         getAll();
        return "";
    }

      public String update() {

          materielBO.update(this);
          getAll();
        return "";
    }


       public void  getAll() {
           setListe(materielBO.getAll());
    }


}

MaterielImplBO.java:

public class ImplMateriel extends HibernateDaoSupport implements IfaceMateriel {
@Autowired
public SessionFactory sessionFactory;

    @Override
    @Transactional
    public void insert(Materiel obj) {
        getHibernateTemplate().save(obj);
    }

    @Override
    public void delete(Materiel obj) {
        getHibernateTemplate().delete(obj);}

    @Override
    public void update(Materiel obj) {
        getHibernateTemplate().merge(obj);
        }

    @Override
    @Transactional
    public List<Materiel> getAll() {
        return (List<Materiel>) (Object)getHibernateTemplate().find("from Materiel"); }

}

0 个答案:

没有答案