一个简单的SSH(struts2.1 + hibernate3.3 + spring3.0)例子改为用注解配置后启动tomcat报错:
 Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAction' defined in file [C:\Program Files\Apache Software Foundation\apache-tomcat-7.0.5\webapps\ssh5\WEB-INF\classes\com\lm\struts2\userAction.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.lm.struts2.userAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:892)
.............
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.lm.struts2.userAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
..................
Caused by: java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.getRequest(ServletActionContext.java:112)
at com.lm.struts2.userAction.<init>(userAction.java:21)意思好像是不能初始化UserAction下面是配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" 
 default-autowire="byName">

<context:annotation-config />
<context:component-scan base-package="com.lm"></context:component-scan>    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306"></property>
<property name="username" value="root"></property>
<property name="password" value="lm"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
</bean>struts.xml:<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts><package name="suibiaojiao" extends="struts-default" namespace="/" >
   <action name="*_*" class="com.lm.struts2.{1}Action" method="{2}" >
      <result name="ok"  type="redirect">user_findAll.action</result>
      <result name="error">/error.jsp</result>
      <result name="findAllOK">/studentList.jsp</result>
      <result name="deleteOK" type="redirect">user_findAll.action</result>
      <result name="input" >/save.jsp</result>  
      <result name="updatePOK" >/update.jsp</result>  
      <result name="updateOK" type="redirect">user_findAll.action</result>  
   </action>
 </package>
</struts> 
StudentDAO:package com.lm.Impdao;import java.util.List;import javax.annotation.Resource;import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;import com.lm.Idao.IStudentDAO;
import com.lm.dao.Student;@Repository
public class StudentDAO extends HibernateDaoSupport implements IStudentDAO{
       //此处代码省略}
StudentBiz:package com.lm.Impbiz;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;import javax.annotation.Resource;import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;import com.lm.Idao.IStudentDAO;
import com.lm.biz.IStudentBiz;
import com.lm.dao.Student;@Service
public class StudentBiz implements IStudentBiz {
@Resource
private IStudentDAO studentDAO;
public void setStuDAO(IStudentDAO stuDAO) {
this.studentDAO = stuDAO;
}
public boolean login(Student stu) {
return studentDAO.findByExample(stu).size() > 0;
}
public List<Student> findAll() {
return studentDAO.findAll();
}

public boolean save(Student stu){
boolean flag=false;
try{
if(studentDAO.findByExample(stu).size() == 0){
studentDAO.save(stu);
         flag=true;
}
}catch(Exception ex){
}

return flag;
}

public boolean delete(int stuId){
boolean flag=false;
try{
studentDAO.delete(stuId);
    flag=true;
}catch(Exception ex){
ex.printStackTrace();
}
return flag;
}
public Student findById(int stuId) {
return studentDAO.findById(stuId);
 
}
public boolean update(Student stu) {
if(studentDAO.merge(stu) != null)
     return true;
else
return false;
}
}UserAction:
package com.lm.struts2;import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;import com.lm.biz.IStudentBiz;
import com.lm.dao.Student;
import com.opensymphony.xwork2.ActionSupport;@Controller
public class userAction extends ActionSupport {

HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=request.getSession();

@Resource
private Student stu;
private IStudentBiz studentBiz;


public void setStuBiz(IStudentBiz stuBiz) {
this.studentBiz = stuBiz;
} public Student getStu() {
return stu;
} public void setStu(Student stu) {
this.stu = stu;
}


public String save() throws Exception{
String tag="error";
if(studentBiz.save(stu))
tag="ok";
return tag;
}

public String findAll() throws Exception{
String tag="error";
try{
    List<Student> stus = studentBiz.findAll();
    tag="findAllOK";
    request.setAttribute("stus", stus);
}catch(Exception ex){
ex.printStackTrace();
}

return tag;
}

public String delete() throws Exception{
String tag="error";
try{
if(studentBiz.delete(stu.getStuId()))
        tag="deleteOK";
}catch(Exception ex){
ex.printStackTrace();
}

return tag;
}

   public String updateP() throws Exception{
   stu=studentBiz.findById(stu.getStuId());
   return "updatePOK";
}
   
public String updatesave() throws Exception{
String tag="error";
if(studentBiz.update(stu))
tag="updateOK";
return tag;
}

  }

解决方案 »

  1.   

    <context:component-scan base-package="com.lm"></context:component-scan>
    你用的这个:但是你的service 及action里面没有配置@componment注解的。类似这种:Action里面
    @Component(value="answerAction")
    @Scope("prototype")
    public class AnswerAction extends BaseAction {
    service 里面类似:
    @Component(value="testAddTaxService")
    public class TestAddTaxService implements ITestAddTaxService {
      

  2.   

    Constructor threw exception; nested exception is 
    注入的时候出问题了
    set方法会自动调用的
    你去掉set方法试试看在set的地方打断点看看,注入的为空
    加入注解后就不要写set的了
      

  3.   

     [com.lm.struts2.userAction]: Constructor threw exception; nested exception is java.lang.NullPointerException空指针异常
    set方法注入失败
    去掉set
    注解后就不要写set方法了在set方法debug下