我正在學習Spring和mybatis。spring無法在mybatis中自動裝入NullPointerException
我遇到了一個問題。我主要是從officail tutorial得知,但是我不明白我在做什麼。
春天不能autowire UserMapper在ServiceImpl。
並有一個例外,其是
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中打印It is in setUserdao
或getUserdao
。
但是沒有什麼,只有11111111
當userdao
爲空。
所以任何人都可以幫助我嗎?謝謝。
春*「不能自動裝配」 *,因爲你不告訴它來 - 你必須加上'@ Autowired'上述任一'私人userDAO的UserDAO的; '或'public void setUserdao(UserDao userdao){'。既然你已經在使用XML,你也可以將它指定爲'
kryger
想你。但我在'private UserDao userdao'上面加'@ Autowired',就有同樣的例外。 – jack
這是因爲你註釋掉了'component-scan'。請注意,MyBatis與您的問題無關,您可以在此處輕鬆找到答案,或者最好是Spring的官方文檔。 – kryger