在java中为已经引用的bean Spring mvc访问bean时出现空指针异常

时间:2016-03-29 12:17:48

标签: java spring hibernate 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); 具有与模板对应的空指针异常。

请告诉我我的错误。

1 个答案:

答案 0 :(得分:1)

在FrontController类中,您应该自动装配(或从应用程序上下文获取)EmployeeDao。