spring @autowired nullpointer

时间:2017-03-01 12:41:27

标签: spring autowired

我在ManagerBean中有一个Spring @Autowired注释。但是我的注释是null,Where is de error?

这是我的豆子

package es.juntadeandalucia.capder.plja.controlador;

import.....

@ManagedBean(name = "kk")
@RequestScoped
public class PruebaBean implements Serializable {

   private static final long serialVersionUID = 654564654L;

   String                    a;

   String                    b;

   @Autowired // This var is null, Why?
   private PersonaServicio   personaServicio;

   @PostConstruct
   public void init() {
   }

   public PruebaBean() {
      this.a = "1";
      this.b = "Raúl";
   }

   public List<Persona> getPersonas() {
      // this.personaServicio is null, Why?
      return this.personaServicio.obtenerPersonas();
   }        
}

这是我的界面

package es.juntadeandalucia.capder.plja.negocio.servicio;

import.....    

public interface PersonaServicio {

   public List<Persona> obtenerPersonas();

}

及其实施

package es.juntadeandalucia.capder.plja.negocio.servicio.impl;

import.....    

@Service
public class PersonaServicioImpl implements PersonaServicio, Serializable {

   private static final long serialVersionUID = 8035927808046807526L;

   private PersonaDAO        personaDAO;

   public PersonaServicioImpl() {

   }

   public PersonaDAO getPersonaDAO() {
      return this.personaDAO;
   }

   public void setPersonaDAO(PersonaDAO personaDAO) {
      this.personaDAO = personaDAO;
   }

   @Override
   public List<Persona> obtenerPersonas() {
      // Is not important for this example
      return null;
   }

}

现在是Index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<f:loadBundle basename="resources.application" var="msg" />

<h:head>
    <title><h:outputText value="hi" /></title>
</h:head>
<h:body>
        <p:dataTable value="#{kk.personas}" var="persona" emptyMessage="Sin personas">
            <p:column headerText="Id">
                <h:outputText value="#{persona.id}" />
            </p:column>
            <p:column headerText="Nombre">
                <h:outputText value="#{persona.nombre}" />
            </p:column>
            <p:column headerText="Apellido">
                <h:outputText value="#{persona.apellidos}" />
            </p:column>

        </p:dataTable>

    </h:form>
</h:body>
</html>

并完成applicationContext.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <context:component-scan base-package="es.juntadeandalucia.capder.plja"/>   

    <context:annotation-config  />      

</beans>

错误是(通过Tomcat v7):

GRAVE: Error Rendering View[/index.xhtml]
javax.el.ELException: /index.xhtml @34,81 value="#{kk.personas}": Error reading 'personas' en el tipo es.juntadeandalucia.capder.plja.controlador.PruebaBean
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIData.getValue(UIData.java:732)
    at org.primefaces.component.api.UIData.getDataModel(UIData.java:764)
    at javax.faces.component.UIData.getRowCount(UIData.java:356)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:832)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:814)
    at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:290)
    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:252)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:87)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:651)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.ELException: Error reading 'personas' en el tipo es.juntadeandalucia.capder.plja.controlador.PruebaBean
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:110)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:184)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    ... 43 more
Caused by: java.lang.NullPointerException
    at es.juntadeandalucia.capder.plja.controlador.PruebaBean.getPersonas(PruebaBean.java:41)
    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:497)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:99)
    ... 48 more

谢谢大家的时间

2 个答案:

答案 0 :(得分:0)

你告诉过春天扫描这个包吗?代码看起来很好所以应该是你不会告诉spring扫描类。这是因为只添加注释,不能修复或促进依赖注入,Spring需要知道在哪里寻找。

您可以在spring应用程序的配置类上使用@ComponentScan("yourPackage")来指示spring扫描。

@Service, @Component等注释添加元描述。 Spring只注入那些创建为bean或用注释标记的类的实例。

修改 创建一个类似于下面的类

@EnableWebMvc
@ComponentScan(basePackages = {"your package"})
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//your code here
}

答案 1 :(得分:0)

要最直接回答您的问题,我认为您需要将PruebaBean添加到@ManagedBean课程。

我不确定如果你也使用类似JEE服务器的东西,.square { background-color: yellow; border: 1px solid #000000; width: 300px; height: 300px; }暗示,但这可能是一个单独的主题