【问题标题】:Why is my @Autowired object null?为什么我的 @Autowired 对象为空?
【发布时间】:2014-06-21 05:12:14
【问题描述】:

我有以下 JSF 托管 bean:

package com.example;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ManagedBean
public class Bean implements Serializable {

    @Autowired
    private SomeService someService;

    public void update() {
        someService.update(someEntity);
    }

    // ...
}

以及下面的 Spring 服务:

@Transactional
@Repository
public class SomeService {

    @Autowired
    private SessionFactory sessionFactory;

    // ...
}

Spring 配置如下:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <description>Example configuration to get you started.</description>

    <context:component-scan base-package="com.example" />

    <!-- To add exception translation to a template-less Hibernate DAO -->
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <context:annotation-config />

    <!-- Database Configuration -->
    <import resource="database/DataSource.xml" />
    <import resource="database/Hibernate.xml" />
    <tx:annotation-driven />
 </beans>

当我调用 JSF 托管 bean update() 方法时,它会抛出一个 NullPointerException:

javax.faces.FacesException: #{bean.update()}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
    at org.springframework.faces.webflow.FlowActionListener.processAction(FlowActionListener.java:71)
    at org.springframework.faces.model.SelectionTrackingActionListener.processAction(SelectionTrackingActionListener.java:55)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    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:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:191)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
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:101)
    ... 28 more
Caused by: java.lang.NullPointerException
    at com.example.Bean.update(Bean.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 29 more

换句话说,someService 是null。我终其一生都无法弄清楚为什么。我在我的 bean 顶部声明了@Component。所以我希望@Autowired 能够正常工作。任何帮助都会很棒!

【问题讨论】:

  • 你能发布你的配置文件吗?你在使用组件扫描吗?
  • 我正在使用组件:扫描。当我尝试同一个包中的另一个类时,我没有得到空指针。我已经发布了我的配置文件。
  • 你还用@Component注释了FilmBo?
  • 不,我没有。但是,我试过了,并没有解决问题。
  • 另外请贴出调用代码的类。

标签: spring jsf nullpointerexception autowired managed-bean


【解决方案1】:

您不能像在这里尝试的那样混合注释和上下文。

首先,要将 Spring bean(或实际上任何 bean)注入 JSF 托管 bean,请使用 @ManagedProperty 注释。因此,如果其他一切设置正确(Spring-wise,包括 Spring Faces EL 解析器),您应该:

@ManagedProperty("#{someService}")
private SomeService someService;

当您将一个类声明为@ManagedBean 时,它不在 Spring 的上下文中。结果@Autowired在这里没有权力; spring 不能将任何东西注入到其上下文之外的任何东西中。

如果你想使用@Component和@Autowired这对,你需要去掉@ManagedBean,使它成为一个Spring托管bean而不是JSF托管bean。

【讨论】:

  • 不完全正确; Spring 可以通过其他方式获取对象。具体来说。 @Configurable 可能会满足他的需要。
  • @chrylis- 这很有趣。你有这方面的资源吗(春天注入到不在其上下文中的 bean 中)?
  • 检查我的副本;我基本上写了一篇文章,并有一个示例项目。
猜你喜欢
  • 2022-01-04
相关资源
最近更新 更多