action层的代码
@Component("user")
public class UserAction extends ActionSupport {
private UserManager userManager;
private String username;
private String password;

public UserManager getUserManager() {
return userManager;
}
@Resource
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}

@Override
public String execute() throws Exception {
User user = new User();
user.setUsername(username);
user.setPassword(password);
if(userManager.existUser(user)){
return "fail";
}
userManager.add(user);
return "success";
}
......
}service层中的代码
@Component("userManager")
public class UserManager {
private UserDao userDao;
private UserLogDao userLogDao;
...}spring配置文件中使用
<context:annotation-config />
<context:component-scan base-package="com" />--------------------------------------------------------------------------------
单元测试service层(只是spring)没报错
测试struts2时报错,类型为空指针
位置为action层中的
if(userManager.existUser(user)){
--------------------------------------------------------------------------------
我总怀疑struts和spring没有连接起来,userManager没有被注入,问题一直解决不了

解决方案 »

  1.   

    你好,sorry,错误为:
    严重: Servlet.service() for servlet default threw exception
    java.lang.NullPointerException
    at com.action.UserAction.execute(UserAction.java:31)
      

  2.   

    @Autowired
    private UserManager userManager;
      

  3.   


    你好,还是同样的错误,出错位置if(userManager.existUser(user)){
      

  4.   

    你是否配置了Spring容器,在web.xml中配置如下
      <listener>
          <listener-class>
                 org.springframework.web.context.ContextLoaderListener
          </listener-class>
      </listener>
      

  5.   

    肯定没有注入进来
    把配置l文件全贴出来看看
    顺便看一下有没有把struts-spring-plugin.jar包加进来
      

  6.   

    setUserManager的问题吧,好像声明一个 UserManager的变量加上源数据@Resource就ok了 不用在set get了,当然如果前后台传递的数据还是需要set get的
      

  7.   

    配置文件中设置了“事务bean”没啊? 
    我以前也遇到过这样问题,配置下配置文件就行了
      

  8.   

    自动注入的话 要是接口吧。UserManager要是接口,然后写一个他的实现类。
      

  9.   


    你好,我把源码传上来了,帮忙看一下,下载地址:http://download.csdn.net/detail/kxf327/4055771
    不胜感激!
      

  10.   


    你好,我把源码传上来了,帮忙看一下,下载地址:http://download.csdn.net/detail/kxf327/4055771
    不胜感激!
      

  11.   

    spring的配置文件也贴出来看看……
      

  12.   

    <?xml version="1.0" encoding="UTF-8"?> <context:annotation-config />
    <context:component-scan base-package="com" /> <bean id="logIntercepter" class="com.aop.LogInterceptor"></bean>
    <!-- 日志管理AOP -->
    <aop:config>
    <aop:aspect id="logAspect" ref="logIntercepter">
    <aop:before method="before" pointcut="execution(* com.service..*.add(..))" />
    <aop:after method="after" pointcut="execution(* com.service..*.add(..))" />
    </aop:aspect>
    </aop:config>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver">
    </property>
    <property name="url" value="jdbc:mysql://localhost:3306/spring">
    </property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
    <property name="maxActive" value="100"></property>
    <property name="maxIdle" value="10"></property>
    <property name="maxWait" value="10"></property>
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property> <!-- <property name="annotatedClasses"> <list> <value>com.model.User</value> 
    <value>com.model.Userlog</value> </list> </property> --> <property name="packagesToScan">
    <list>
    <value>com.model</value>
    <!-- <value>com.model.*</value> -->
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop> <!-- 显示sql语句 -->
    </props>
    </property>
    </bean>
    <!-- 事务管理 -->
    <!-- <tx:annotation-driven transaction-manager="txManager"/> -->
    <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean> <!-- XML方式事务配置 -->
    <aop:config>
    <aop:pointcut expression="execution(public * com.service.*.*(..))"
    id="businessService" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
    </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="getUser" read-only="true" />
    <!-- <tx:method name="exist*" read-only="true" /> -->
    <tx:method name="add*" /> <!-- 默认propagation是REQUIRED -->
    </tx:attributes>
    </tx:advice>
      

  13.   

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/springContext/applicationContext.xml</param-value>
    </context-param>
    搞了半天是路径错误