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);
}

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

13个回答

你把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>
hahaqingfeng1
hahaqingfeng1 回复念去去云: 那怎么才能启动呢?
接近 4 年之前 回复
z947511564
念去去云 设置监听,也不行的
接近 4 年之前 回复

图片说明

加入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());

}
hahaqingfeng1
hahaqingfeng1 好的
接近 4 年之前 回复

junit 换成4.9版本以上的。

hahaqingfeng1
hahaqingfeng1 回复念去去云: 注解扫描配的有,我怀疑jar包不兼容,谢谢了,先不纠结这个问题了。
接近 4 年之前 回复
z947511564
念去去云 回复hahaqingfeng1: 你没有配置spring的注解扫描
接近 4 年之前 回复
z947511564
念去去云 回复hahaqingfeng1: <context:component-scan base-package="cc.yihy"> </context:component-scan> <context:annotation-config />
接近 4 年之前 回复
hahaqingfeng1
hahaqingfeng1 回复念去去云: 我用annotation(@component,@resource)方式来写,userManager还是不能注入到action中,但是用ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId")可以拿到userManager了,什么原因呢,我怀疑是struts2-spring-plungin.jar的问题,你能给我推荐一下能整合到一块的SSH版本吗?
接近 4 年之前 回复
z947511564
念去去云 回复hahaqingfeng1: ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("beanId");
接近 4 年之前 回复
z947511564
念去去云 回复hahaqingfeng1: 现在报什么错
接近 4 年之前 回复
hahaqingfeng1
hahaqingfeng1 到底是哪的问题呢
接近 4 年之前 回复
hahaqingfeng1
hahaqingfeng1 我用的jUnit4.11,不行。
接近 4 年之前 回复

图片说明

共13条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐