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

标签 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); 存在与模板对应的空指针异常。

请让我知道我犯了什么错误。

最佳答案

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

关于java - 在 java 中访问已经引用的 bean Spring mvc 中的 bean 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284061/

相关文章:

java - 在建议之前和之后传递对象?

java - Hive-开始使用hadoop 2.7.3和derby的问题

java - 使用 Hibernate 和 JDBC 关闭连接

hibernate - 使用实体管理器刷新时事务中空闲

java - Spring Data JPA + Hibernate + PostgreSQL

java - 使用同步块(synchronized block)预订座位

java - WebSphere Liberty Profile AdminClient 连接

java - 使用 Intellij IDEA 运行项目时找不到 keystore.jks(用于 SSL),但被 Eclipse 找到

spring - Spring登录时显示错误消息

java - 如何使用@StartNode 关系查询 Neo4jRepository?