struts2与spring整合后测试时报空指针错误,急死了,跪求高手指点,非常感谢!

beans.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>




<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="annotatedClasses">
        <list>
            <value>com.bjsxt.registration.model.User</value>

        </list>
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">
                org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="userDao" class="com.bjsxt.registration.dao.impl.UserDaoImpl"
    scope="prototype">

</bean>
<bean id="userManager" class="com.bjsxt.registration.service.impl.UserManagerImpl"
    scope="prototype">
    <!-- 注入UserDao -->
    <property name="userDao" ref="userDao" />
</bean>


<bean id="user" class="com.bjsxt.registration.action.UserAction"
    scope="prototype">
    <!-- 注入Service -->
    <property name="userManager" ref="userManager" />
</bean>

<bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<aop:config>
    <aop:pointcut id="bussinessService"
        expression="execution(public * com.bjsxt.registration.service.*.*(..))" />
    <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="exists" read-only="true" />
        <tx:method name="add*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

UserAction类中的代码如下:
package com.bjsxt.registration.action;

import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.UserManager;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

private String username;
private String password;
private String password2;

private UserManager userManager;

public UserManager getUserManager() {
    return userManager;
}

public void setUserManager(UserManager userManager) {
    this.userManager = userManager;
}

@Override
public String execute() throws Exception {
    User u = new User();
    u.setUsername(username);
    u.setPassword(password);
    if (userManager.exists(u)) {
        return "fail";
    }
    userManager.add(u);
    return "success";
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPassword2() {
    return password2;
}

public void setPassword2(String password2) {
    this.password2 = password2;
}

}
UserDaoImpl类中的代码如下
package com.bjsxt.registration.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate4.HibernateTemplate;
import com.bjsxt.registration.dao.UserDao;
import com.bjsxt.registration.model.User;

public class UserDaoImpl implements UserDao {

private HibernateTemplate hibernateTemplate;

public void save(User u) {
    hibernateTemplate.save(u);

}

public boolean checkUserExistsWithName(String username) {
    List<User> users = (List<User>) hibernateTemplate.find("from User u where u.username = '" + username + "'");

    if (users != null && users.size() > 0) {
        return true;
    }
    return false;
}

public HibernateTemplate getHibernateTemplate() {
    return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
}

}

UserManagerImpl类中的代码如下
package com.bjsxt.registration.service.impl;

import com.bjsxt.registration.dao.UserDao;
import com.bjsxt.registration.model.User;
import com.bjsxt.registration.service.UserManager;

public class UserManagerImpl implements UserManager {

private UserDao userDao;

public UserDao getUserDao() {
    return userDao;
}

public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

/*
 * (non-Javadoc)
 * 
 * @see com.bjsxt.registration.service.impl.UserManager#exists(com.bjsxt.
 * registration.model.User)
 */
public boolean exists(User u) throws Exception {
    return userDao.checkUserExistsWithName(u.getUsername());

}

/*
 * (non-Javadoc)
 * 
 * @see com.bjsxt.registration.service.impl.UserManager#add(com.bjsxt.
 * registration.model.User)
 */
public void add(User u) throws Exception {
    userDao.save(u);
}

}
测试异常如下:
图片说明

你把debug的错误信息能不能贴全。

图片说明

是不是jar包有问题呢,图片说明图片说明

额,spring都没启动,userManager没注入,肯定空啊!

web.xml中设置的有监听啊,
org.springframework.web.context.ContextLoaderListener
<!-- default: /WEB-INF/applicationContext.xml -->

<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

刚粘错了,是这个,


org.springframework.web.context.ContextLoaderListener
<!-- default: /WEB-INF/applicationContext.xml -->

<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
    <param-value>classpath:beans.xml</param-value>
</context-param>

图片说明

加入spring Test包
让你的测试类继承这个类,注解上的xml改成你的

 @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
public class SpringTestBase extends AbstractJUnit4SpringContextTests{
    public Logger log = LoggerFactory.getLogger(this.getClass());

}

junit 换成4.9版本以上的。

图片说明

图片说明,这是执行的结果,你看下

![图片说明](https://img-ask.csdn.net/upload/201608/20/1471683719_23551.jpg)图片说明
是这样的

图片说明