日期:2014-05-17  浏览次数:20693 次

SSH使用注解方式出异常 求教
一个简单的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 code

<?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>