qq_29122341
萍凡如我
采纳率0%
2015-10-05 13:06 浏览 2.2k

spring3+mybatis 注入空指针问题,请各位帮忙一下。

大家好,我刚学习三大框架,碰到一个空指针问题怎么都解决不了,快3天了,请帮忙下,谢谢了。
我测试所有的注入都是空指针

applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jdbc=" http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/jdbc   
           http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
    default-autowire="byName" default-lazy-init="false">

    <!-- 开启自动扫描并注册Bean定义支持 -->
    <!-- <context:component-scan base-package="cn.xhf"></context:component-scan> -->

    <!-- 采用注释的方式配置bean -->
    <!-- <context:annotation-config /> -->

    <!-- 配置datasource源 -->
    <context:property-placeholder location="classpath:mysql.properties" />

    <!-- 使用mysql配置属性值 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="maxActive" value="${maxActive}"></property>
        <property name="maxIdle" value="${maxIdle}"></property>
        <property name="maxWait" value="${maxWait}"></property>
        <property name="defaultAutoCommit" value="${defaultAutoCommit}"></property>
    </bean>

    <!-- 配置sqlSessionFactory,同时制定数据源 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:Configuration.xml" />
    </bean>

    <!-- 配置SqlSessionTemplate -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="BATCH" />
    </bean>

    <!-- 配置dao -->
    <bean id="booksDao" class="cn.xhf.dao.impl.BooksDaoImpl">
        <property name="sqlSession" ref="sqlSession"></property>
    </bean>
    <bean id="usersDao" class="cn.xhf.dao.impl.UsersDaoImpl">
        <property name="sqlSession" ref="sqlSession"></property>
    </bean>

    <!-- 配置service -->
    <bean id="booksService" class="cn.xhf.service.impl.BooksServiceImpl"
        scope="prototype">
        <property name="booksDao" ref="booksDao"></property>
    </bean>

    <bean id="usersService" class="cn.xhf.service.impl.UsersServiceImpl"
        scope="prototype">
        <property name="usersDao" ref="usersDao"></property>
    </bean>

    <!-- 配置action -->
    <bean id="booksAction" class="cn.xhf.action.BooksAction" scope="prototype">
        <property name="booksService" ref="booksService"></property>
    </bean>

    <bean id="usersAction" class="cn.xhf.action.UsersAction" scope="prototype">
        <property name="usersService" ref="usersService"></property>
    </bean>


    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="select*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 切面配置 -->
<!--    <aop:config>
        <aop:pointcut expression="execution(* cn.xhf.service.*.*(..))"
            id="method" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="method" />
    </aop:config> -->

</beans>

dao层

 package cn.xhf.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;

import cn.xhf.dao.BooksDao;
import cn.xhf.pojo.Books;
/**
 * 
 */
public class BooksDaoImpl implements BooksDao {

    private SqlSession sqlSession;  //就是这个空指针 ,其他bean也是空

    @Override
    public int deleteBooksById(String bid) {
        return sqlSession.delete("books.deleteBooksById", bid);
    }


    @Override
    public int updataBooksById(String bid) {
        return sqlSession.update("books.updataBooksById", bid);
    }

    @Override
    public List<Books> findAll() {
        List<Books> booksList = new ArrayList<Books>();
        booksList = sqlSession.selectList("books.findBooks");
        return booksList;
    }

    @Override
    public List<Books> findBooksByKeyword(String bookName, String bookAuthor) {
        List<Books> bookList = new ArrayList<Books>();
//      Books books = new Books();
//      books.setBookName(bookName);
//      books.setBookAuthor(bookAuthor);
//      bookList = sqlSession.selectList("books.findBooks", books);
        return bookList;
    }

    @Override
    public int insertBooks(Books books) {
        return sqlSession.insert("books.insertBooks", books);
    }

    public SqlSession getSqlSession() {
        return sqlSession;
    }

    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

}

test方法

 package cn.xhf.pojo;

import java.util.List;

import cn.xhf.dao.BooksDao;
import cn.xhf.dao.impl.BooksDaoImpl;

public class TestBooks {
    public static void main(String[] args) {
        BooksDao booksDao = new BooksDaoImpl();
        List<Books> list = booksDao.findAll();
        System.out.println(list.size());
    }
}

报错信息:
Exception in thread "main" java.lang.NullPointerException
at cn.xhf.dao.impl.BooksDaoImpl.findAll(BooksDaoImpl.java:32)
at cn.xhf.pojo.TestBooks.main(TestBooks.java:11)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • wojiushiwo945you 毕小宝 2015-10-05 14:04

    首先, spring作为容器就是为了解决代码中大量new操作的,因为new操作涉及到其他依赖对象的创举,你的TestBooks就是自己new操作的,但是没有设置各个依赖对象,所以会报空指针。
    因为你的代码是自己new的BookDao对象,而不是靠Spring容器创建的bean对象,BookDao对象中的依赖的其他对象当然不会自动被注入了。
    其次,依赖bean是由Spring注入的,所以在创建实例时应该通过spring容器创建 而不是自己new操作来创建实例。Spring创建对象的方法:
    修正你的main方法如下:

     ApplicationContext context= new FileSystemXmlApplicationContext("applicationContext.xml"); 
     BooksDao booksDao = context.getBean("booksDao");
    
    

    这样通过spring容器创建的bean的依赖对象才会被自动注入了。

    点赞 评论 复制链接分享
  • qq_29608405 Guan.shuang 2015-10-05 14:48

    我来看解决办法的。。。。同是初学者。。。

    点赞 评论 复制链接分享

相关推荐