簡體   English   中英

在Java中訪問已引用的bean的java中的bean時出現空指針異常Spring MVC

[英]Null pointer exception while accessing bean in java for already referred bean Spring mvc

這是我的dispatcher-servlet.xml的代碼片段

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.*"></context:component-scan>
    <context:annotation-config />
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean name="viewResolver" id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <bean name="dataSource" id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/com"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <bean id="sessionFactoryBean" name="sessionFactoryBean" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
            </list>
        </property>
    </bean>
    <bean id="template" name="template" class="org.springframework.orm.hibernate3.HibernateTemplate" autowire-candidate="true">
        <property name="sessionFactory" ref="sessionFactoryBean"></property>
    </bean>
    <bean id="EmployeeDao" name="EmployeeDao" class="com.dao.EmployeeDaoImpl">
        <property name="template" ref="template"></property>
    </bean>
</beans>

這是我的EmployeeDaoImpl的代碼片段

public class EmployeeDaoImpl  implements EmployeeDao{
    HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }
    public boolean saveDummyEmployees(List<Employee> employees) {
        try{
            template.saveOrUpdateAll(employees);
            return true;
        }catch(DataAccessException ex){ 
            ex.printStackTrace();
            return false;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

此行template.saveOrUpdateAll(employees); 具有與模板相對應的空指針異常。

請讓我知道我在犯什么錯誤。

在FrontController類中,應該自動裝配EmployeeDao(或從應用程序上下文獲得)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM