0

I am learning Spring and mybatis.

And I encountered a problem.I mainly learn from officail tutorial.But I can't get what i what.

The spring can't autowire UserMapper in ServiceImpl.

And there is a Exception which is

java.lang.NullPointerException

    main.java.cn.qingtianr.service.impl.UserServiceImpl.findByUserName(UserServiceImpl.java:23)
    main.java.cn.qingtianr.action.UserAction.execute(UserAction.java:22)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
    ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
    ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
    ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
    ognl.ASTMethod.getValueBody(ASTMethod.java:90)
    ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    ognl.SimpleNode.getValue(SimpleNode.java:258)
    ognl.Ognl.getValue(Ognl.java:494)

And other source are there.

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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="archivesi" class="main.java.cn.qingtianr.service.impl.ArchiveServiceImpl"/>
    <bean id="articlesi" class="main.java.cn.qingtianr.service.impl.ArticleServiceImpl"/>
    <bean id="usersi" class="main.java.cn.qingtianr.service.impl.UserServiceImpl"/>

    <!--<aop:aspectj-autoproxy proxy-target-class="true" />-->
    <!-- 自动扫描 -->
    <!--<context:component-scan base-package="main.java.cn.qingtianr" />-->
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:main/resources/jdbc.properties" />
    </bean>

    <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="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最大空闲 -->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="userdao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="main.java.cn.qingtianr.dao.UserDao" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>


        </beans>

UserServiceImpl.java

package main.java.cn.qingtianr.service.impl;

import main.java.cn.qingtianr.dao.UserDao;
import main.java.cn.qingtianr.dbc.MybatisSqlSessionFactory;
import main.java.cn.qingtianr.model.User;
import main.java.cn.qingtianr.service.UserService;

/**
 * Created by jack on 16-3-29.
 */
public class UserServiceImpl implements UserService{

    private UserDao userdao;

    public User findByUserName(String username) throws Exception
    {
        User user = null;
        try
        {
            if(userdao == null){
                System.out.println("11111111111");
            }
            user = this.userdao.findByUserName(username);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            MybatisSqlSessionFactory.closeSession();
        }
        return user;
    }

    public UserDao getUserdao() {
        System.out.println("It is in getUserdao");
        return userdao;
    }

    public void setUserdao(UserDao userdao) {
        System.out.println("It is in setUserdao");
        this.userdao = userdao;
    }
}

UserDao.java

package main.java.cn.qingtianr.dao;

import main.java.cn.qingtianr.model.User;

/**
 * Created by jack on 16-3-29.
 */
public interface UserDao {
    public User findByUserName(String username) throws Exception;
}

UserDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="main.java.cn.qingtianr.dao.UserDao">

    <select id="findByUserName" parameterType="int" resultType="main.java.cn.qingtianr.model.User">
        SELECT * FROM user WHERE username = #{username}
    </select>

</mapper>

If spring autowire mybatis Mapper then there will printIt is in setUserdao or getUserdao in UserServiceImpl.java.

But There is nothing but 11111111 when userdao is null.

So can anyone help me? thx.

jack
  • 5
  • 7
  • Spring *"can't autowire"* because you're not telling it to - you'll have to add `@Autowired` above either `private UserDao userdao;` or `public void setUserdao(UserDao userdao) {`. Since you're already using XML you could also specify it as a property of ` – kryger Apr 15 '16 at 13:39
  • Think you.But I add `@Autowired` above `private UserDao userdao`,there is the same exception. – jack Apr 15 '16 at 13:57
  • This is because you commented out the `component-scan`. Note that MyBatis is not relevant to your question, you could easily find the answers on here on SO or, preferably, Spring's official documentation. – kryger Apr 15 '16 at 14:00
  • It work for me.thank you,I have thought this question for a whole afternoon.I think Spring's official documentation is what i ignored. – jack Apr 15 '16 at 14:10

2 Answers2

0
<bean id="usersi" class="main.java.cn.qingtianr.service.impl.UserServiceImpl">
    <property name="userdao" ref = "userdao"/>
</bean >
kryger
  • 12,906
  • 8
  • 44
  • 65
Shivakumar
  • 71
  • 8
-1

You're not setting the userDao bean in your usersi bean definition. Take a look here for more info: http://www.mybatis.org/spring/mappers.html

Michael Peacock
  • 2,011
  • 1
  • 11
  • 14