spring mybatis NullPointerException 无法自动装配
spring can't autowire in mybatis NullPointerException
正在学习Spring和mybatis.
而我遇到了一个problem.I主要是借鉴officail tutorial。但是我什么也得不到。
spring 无法在 ServiceImpl.
中自动装配 UserMapper
还有一个例外是
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)
还有其他来源。
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>
如果spring autowire mybatis Mapper 那么会在UserServiceImpl.java.[=21=中打印It is in setUserdao
或getUserdao
]
但是当 userdao
为 null 时,除了 11111111
什么都没有。
所以有人可以帮助我吗?谢谢
您没有在 usersi bean 定义中设置 userDao bean。在这里查看更多信息:http://www.mybatis.org/spring/mappers.html
<bean id="usersi" class="main.java.cn.qingtianr.service.impl.UserServiceImpl">
<property name="userdao" ref = "userdao"/>
</bean >
正在学习Spring和mybatis.
而我遇到了一个problem.I主要是借鉴officail tutorial。但是我什么也得不到。
spring 无法在 ServiceImpl.
中自动装配 UserMapper还有一个例外是
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)
还有其他来源。
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>
如果spring autowire mybatis Mapper 那么会在UserServiceImpl.java.[=21=中打印It is in setUserdao
或getUserdao
]
但是当 userdao
为 null 时,除了 11111111
什么都没有。
所以有人可以帮助我吗?谢谢
您没有在 usersi bean 定义中设置 userDao bean。在这里查看更多信息:http://www.mybatis.org/spring/mappers.html
<bean id="usersi" class="main.java.cn.qingtianr.service.impl.UserServiceImpl">
<property name="userdao" ref = "userdao"/>
</bean >