【问题标题】:Spring Injection getting null pointer ExceptionSpring注入获取空指针异常
【发布时间】:2013-02-22 06:26:47
【问题描述】:

尝试注入对象时出现空指针异常。这是我的代码:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
 </context-param>'

ApplicationContext.XML

<bean id="accessDao" 
 Class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" 
  autowire-candidate="true">
  <property name="transactionManager" ref="txManager" />   
  <property name="target" ref="accessDaoTarget" />   
   <property name="transactionAttributes">   
   <props>   
  <prop key="*">PROPAGATION_REQUIRED</prop>   
   </props>   
  </property>   

</bean>   '

普通豆

import com.domain.dao.IDao;
@Named
public class CommonBean implements Serializable{

/**
 * 
 */

private static final long serialVersionUID = 1L;
@Inject
private IDao accessDao;


public IDao getAccessDao()

      {
        return accessDao;
      }

 public void setAccessDao(IDao accessDao)
  {
    this.accessDao = accessDao;
  }

}

【问题讨论】:

  • 您的 applicationContext.xml 上下文文件中是否定义了“context:component-scan”。
  • s..这里是
  • 你试过用@Autowired代替@Inject吗?
  • 嗨..我试过..但没用..

标签: java spring


【解决方案1】:

我想的原因是组件扫描应该包括 Spring 注释的所有文件。 因此,要使其正常工作,请扩大要扫描的包的范围。

改变

<context:component-scan base-package="com.myjsf.appl.CommonBean" />

 <context:component-scan base-package="com.domain,com.myjsf" />

【讨论】:

  • 感谢您立即回复 Mr.sudhakar..我试过了,但仍然遇到同样的异常
  • 嗨 mohan,他指的是两个不同的包,用逗号分隔。
  • 您能否确保将所有使用 spring 注释的包添加到 标记中,您也可以发布您的 applicationContext.xml 和您的包结构
  • @Karthik:如果对您有帮助,请点赞并标记答案。干杯
【解决方案2】:

我认为原因是因为您指的是实现 IDAO 接口的“accessDao”bean。 在 applicationContext.xml 上声明的 bean accessDao 是 org.springframework.transaction.interceptor.TransactionProxyFactoryBean 类型,它实现了 BeanFactoryAware 接口而不是 IDAO 接口。

因此 spring 将无法识别您尝试注入的 bean (IDAO accessDAO) 并且您的属性将不会被初始化。

【讨论】:

  • 错了。该 bean 将实现由指定的target 定义的接口。
猜你喜欢
  • 1970-01-01
  • 2013-10-07
  • 2012-11-27
  • 2017-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 2013-04-10
相关资源
最近更新 更多