ManagedBean上的注入服务Spring失败了NPE

时间:2014-11-23 21:22:27

标签: java spring hibernate jsf primefaces

使用,Spring 3,Hibernate 4,JSF 2.0,PrimeFaces

我的春天配置:

<context:component-scan base-package="com.tds.erp"/>
<context:annotation-config/>
<context:spring-configured/>

<!-- Data Source Declaration -->
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql:/localhost:3306/erp_tsd" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DataSource" />
    <property name="packagesToScan" value="com.tds.erp.model" />
   <!-- <property name="packagesToScan">
        <list>
            <value>net.javabeat.spring.model</value>
        </list>
    </property>-->

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.DB2400Dialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager"/>

<!-- Transaction Manager is defined -->
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="SessionFactory"/>
</bean>

我的Web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </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>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>

<!-- JSF mapping -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

FacesConfig.xml:

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

ManagedBean:

@ManagedBean(name="UserErpMB")
@Scope(value="request")
 public class UserErpManagedBean implements Serializable {
   private static final long serialVersionUID = 1L;
   private static final String SUCCESS = "success";
   private static final String ERROR   = "error";

//Spring UserErp Service is injected...
   @ManagedProperty("#{IUserErpService}")
    private IUserErpService iUserErpService ; // getter and setter for this. //THIS IS NULL!
  }

我尝试在我的managedBean中注入UserErpService,但它总是为空! NPE

这是我的UserErpServiceImpl:

@Service
@Transactional (readOnly=true)
public class UserErpServiceImpl implements IUserErpService {

@Autowired
IUserErpDao iuserdao;

@Override
@Transactional (propagation = Propagation.REQUIRED, readOnly=false)
public void addUser(UserErp user) {
    // TODO Auto-generated method stub
    System.out.println("Service Impl");
    iuserdao.addUser(user);
}

我遇到了这个例外:

nov. 23, 2014 10:48:06 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
AVERTISSEMENT: #{UserErpMB.addUserErp}: java.lang.NullPointerException
javax.faces.FacesException: #{UserErpMB.addUserErp}: 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:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
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:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:537)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1556)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1513)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
... 29 more
Caused by: java.lang.NullPointerException
at com.tds.erp.managedController.UserErpManagedBean.addUserErp(UserErpManagedBean.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.el.parser.AstValue.invoke(AstValue.java:245)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at            javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 30 more

任何人都可以帮助我?

3 个答案:

答案 0 :(得分:0)

我认为这是问题所在:

@ManagedProperty("#{IUserErpService}")

默认情况下,Spring bean将id作为以小写字母开头的bean(不是接口)的实现类的名称(除非另有手动指定)。在EL中,您通过ID而非接口类引用bean。所以它应该是这样的:

@ManagedProperty("#{userErpServiceImpl}")

答案 1 :(得分:0)

通过添加解决     @零件     @ManagedBean     @SessionScoped

和@Autowired到服务界面:

@Component
@ManagedBean
@SessionScoped
public class UserErpMB {

private static final String SUCCESS = "success";
private static final String ERROR   = "error";

//Spring UserErp Service is injected...
@Autowired
private IUserErpService iUserErpService ; // getter and setter for this.

List<UserErp> usersList;
private int id;
private String name;
private String surname;

答案 2 :(得分:0)

完整答案:

JSF bean:

inject spring service
@ManagedProperty("#{handlerService}")
private HandlerService handlerService = null;
///add setter

春季服务:

@Service("handlerService")
@Component
public class HandlerService {
    @Autowired
    private DomainService domainService;

faces-config.xml
   <application>     
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>                  
    </application>
------------
web.xml

 <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.myspringconfgigclass.CommonCoreConfig</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>