在spring中添加action的bean总是报错

2015-5-20 22:41:31 org.apache.catalina.core.AprLifecycleListener init
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Users\Administrator\AppData\Local\MyEclipse\Common\binary\com.sun.java.jdk.win32.x86_64_1.6.0.013\bin;C:\Users\Administrator\AppData\Local\MyEclipse\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat\bin
2015-5-20 22:41:31 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2015-5-20 22:41:31 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 437 ms
2015-5-20 22:41:31 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2015-5-20 22:41:31 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.13
2015-5-20 22:41:32 org.apache.catalina.loader.WebappClassLoader validateJarFile
信息: validateJarFile(E:\my eclipse_workspace.metadata.me_tcat\webapps\NoteBook\WEB-INF\lib\servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
2015-5-20 22:41:33 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
2015-5-20 22:41:35 org.apache.catalina.core.StandardContext listenerStart
严重: 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 ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ssh2.action.UserAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:276)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:197)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:920)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:883)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1138)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ssh2.action.UserAction]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
... 37 more
Caused by: java.lang.NullPointerException
at org.apache.struts2.ServletActionContext.getResponse(ServletActionContext.java:130)
at com.ssh2.action.UserAction.(UserAction.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
... 39 more
2015-5-20 22:41:35 org.apache.catalina.core.StandardContext start
严重: Error listenerStart
2015-5-20 22:41:35 org.apache.catalina.core.StandardContext start
严重: Context [/SSH2] startup failed due to previous errors
2015-5-20 22:41:35 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
log4j:ERROR LogMananger.repositorySelector was null likely due to error in class reloading, using NOPLoggerRepository.
2015-5-20 22:41:35 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2015-5-20 22:41:35 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2015-5-20 22:41:35 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/18 config=null
2015-5-20 22:41:35 org.apache.catalina.startup.Catalina start
信息: Server startup in 3673

4个回答

检查下你的UserAction代码,com.ssh2.action.UserAction.(UserAction.java:20)异常信息指向这行了。
第20行是不是这句代码HttpSession session = request.getSession();
你的Action实例化的时候,request应该是空的,有可能这行报空指针了。

wojiushiwo945you
毕小宝 回复aaronmorgan: 是的,只有客户端有请求,才会对应有一个HttpServletRequest对象产生的。
4 年多之前 回复
aaronmorgan
AaronJ1 恩,真是这错了,那我要是用request,只能在方法内部获得了???(太感谢了)
4 年多之前 回复

我的代码
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<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/my"></property>
    <property name="username" value="root"></property>
    <property name="password" value="202554"></property>
</bean>

<!-- hibernate的配置文件 -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

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

            <!-- <prop key="hibernate.transaction.factory_class"> org.hibernate.transaction.JDBCTransactionFactory 
                </prop> -->
            <!-- <prop key="hibernate.current_session_context_class"> thread </prop> -->
        </props>
    </property>
    <property name="mappingResources">
        <list>
            <value>com/ssh2/entity/User.hbm.xml</value></list>
    </property></bean>


<bean id="baseDao" class=" com.ssh2.Dao.BaseDao">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>
<bean id="userDao" class=" com.ssh2.Dao.UserDao">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

<bean id="userService" class="com.ssh2.service.UserService">
    <property name="userDao">
        <ref bean="userDao" />
    </property>
</bean>

<bean id="userAction" class="com.ssh2.action.UserAction">
    <property name="userService">
        <ref bean="userService"/>
    </property>
</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 2是否支持动态方法调用,该属性的默认值是true。 -->

<!-- 设置编码方式 -->

<!-- 该属性指定Struts 2中的Action由Spring容器创建。 action中的类不用全路径,而用spring中id -->
<constant name="strutsObjectFactory" value="spring" />


<package name="ssh" namespace="/" extends="struts-default">
    <action name="userAction" class="userAction" method="getByid">
        <result name="success">/success.jsp</result>
        <result name="fail">/fail.jsp</result>
    </action>
</package>

action
<?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 2是否支持动态方法调用,该属性的默认值是true。 -->

<!-- 设置编码方式 -->

<!-- 该属性指定Struts 2中的Action由Spring容器创建。 action中的类不用全路径,而用spring中id -->
<constant name="strutsObjectFactory" value="spring" />


<package name="ssh" namespace="/" extends="struts-default">
    <action name="userAction" class="userAction" method="getByid">
        <result name="success">/success.jsp</result>
        <result name="fail">/fail.jsp</result>
    </action>
</package>

action
package com.ssh2.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh2.entity.User;
import com.ssh2.service.UserService;

public class UserAction extends ActionSupport {
/**
*
*/

private static final long serialVersionUID = 1L;

HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
private User u;
private UserService userService;


public User getU() {
    return u;
}

public void setU(User u) {
    this.u = u;
    System.out.println("setU");
}

public UserService getUserService() {
    return userService;
}

public void setUserService(UserService userService) {
    this.userService = userService;
    System.out.println("setUserService");
}

public String getByid() {
    if(userService==null)
    System.out.println("ddd");

    if(userService.getByid(u.getId())!=null){
        request.setAttribute("user", userService.getByid(u.getId()));
        return "success";
    }
    else
        return "fail";

}

public String login() {
    return "success";
}

}

你看看你的request对象是不是null

aaronmorgan
AaronJ1 只要删除spring配置文件中<bean id="userAction" class="com.ssh2.action.UserAction">就能运行
接近 5 年之前 回复
aaronmorgan
AaronJ1 ,我是按这个搭建的ssh http://xly3000.blog.163.com/blog/static/1324247201231163828803/
接近 5 年之前 回复
aaronmorgan
AaronJ1 我删除了request的那部分,不行,网上说org.springframework.beans.factory.BeanCreationException异常产生的原因是包冲突
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问