Spring dao中的空指针异常,使用bean配置的依赖注入

时间:2014-07-17 06:06:35

标签: spring dependency-injection nullpointerexception

当我尝试使用自动连接bean(使用Setter方法和XML配置的依赖注入)时,我收到以下异常

java.lang.NullPointerException
at com.ecms.controller.service.JobCreationServiceCMODImpl.txSaveData(JobCreationServiceCMODImpl.java:483)
at com.ecms.controller.action.CMODJobCreationAction.doSave(CMODJobCreationAction.java:440)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)

EZM-application.xml中:

   <bean>
    <bean id="cmodJobCreationDao" class="com.ecms.controller.dao.CMODJobCreationDaoImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="jobCreationServiceCMOD" class="com.ecms.controller.service.JobCreationServiceCMODImpl">
    <property name="cmodJobCreationDao">
        <ref bean="cmodJobCreationDao" />
    </property>
</bean>

JobCreationServiceCMODImpl:

public class JobCreationServiceCMODImpl implements JobCreationServiceCMOD{
    /**
     *
     */
    private static final long serialVersionUID = 430594221158425380L;
    private static Log log = LogFactory.getLog(JobCreationServiceCMODImpl.class);
    private CMODJobCreationDao cmodJobCreationDao;

    …..
    /**
     * @return the cmodJobCreationDao
     */
    public CMODJobCreationDao getCmodJobCreationDao() {
        return cmodJobCreationDao;
    }

    /**
     * @param cmodJobCreationDao the cmodJobCreationDao to set
     */
    public void setCmodJobCreationDao(CMODJobCreationDao cmodJobCreationDao) {
        this.cmodJobCreationDao = cmodJobCreationDao;
    }

           public void txSaveData(JobCreationViewCMOD entity) throws Exception {
           JobCreationCMOD creation = new JobCreationCMOD();
        creation.setSrcFolder(entity.getSourceFolder());
        creation.setSrcApplicationGroup(entity.getApplicationGroup());
        creation.setSrcApplication(entity.getApplication());    
           cmodJobCreationDao.persistObject(creation);
           ..

CMODJobCreationDaoImpl:

public class CMODJobCreationDaoImpl extends BaseDaoImpl implements CMODJobCreationDao {

    static String query = "From JobCreationCMOD Where JOBID=?";

    /*
     * (non-Javadoc)
     * @see com.ecms.controller.dao.ProfileCreationDao#fetchRepParam(java.lang.Integer)
     */
    public List<RepositoryParam> fetchRepParams(Integer id) {
        return find(query, new Object[] { id });
    }
}

2 个答案:

答案 0 :(得分:0)

这里你的框架不知道cmodJobCreationDao对象。 cmodJobCreationDao.persistObject(creation);

如果您使用基于注释的弹簧配置,那么您可以自动装配

@Autowired
private CMODJobCreationDao cmodJobCreationDao;

或在您的方法中,您可以按照txSaveData方法中的以下步骤进行操作:

ApplicationContext ctx = new ClassPathXmlApplicationContext("Your xml path");

cmodJobCreationDao = (CMODJobCreationDao) ctx.getBean("cmodJobCreationDao");

答案 1 :(得分:0)

如果你看到stacktrace

      at com.ecms.controller.service.JobCreationServiceCMODImpl.txSaveData(JobCreationServiceCMODImpl.java:483)
      at com.ecms.controller.action.CMODJobCreationAction.doSave(CMODJobCreationAction.java:440)

您看到CMODJobCreationAction.doSave调用JobCreationServiceCMODImpl.txSaveData,我非常确定JobCreationViewCMOD为null。调试并检查此值为null的原因,可能需要在CMODJobCreationAction的bean定义中传递此bean定义。在任何情况下,我都非常确定异常是在这一行中创建的.setSrcFolder(entity.getSourceFolder());因为就像我说的实体是空的