WEB Spring getBean NULL

JustWhat 2010-05-27 01:33:35
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoader</listener-class>
</listener>

<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SpringTestServlet</servlet-name>
<servlet-class>mytest.SpringTestServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SpringTestServlet</servlet-name>
<url-pattern>/servlet/SpringTestServlet</url-pattern>
</servlet-mapping>
</web-app>


beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframeswork.org/dtd/spring-beans.dtd">
<beans>
<bean id="sumbean" class="mytest.Test"></bean>
</beans>

Servlet

public class SpringTestServlet extends HttpServlet {
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException{
ServletContext sc= getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
PrintWriter out = response.getWriter();
int a = Integer.valueOf(request.getParameter("a"));
out.print(a);
out.print("+");
int b = Integer.valueOf(request.getParameter("a"));
out.print(b);
out.print("=");
Test t = (Test)ctx.getBean("sumBean");
out.print(t.sum(a,b));
}
}

public class Test {
public int sum(int x,int y){
return x+y;
}
}


http://localhost:8080/OrderTickets/servlet/SpringTestServlet?a=1&b=2

java.lang.NullPointerException
at mytest.SpringTestServlet.service(SpringTestServlet.java:36)

Test t = (Test)ctx.getBean("sumBean");//36行
...全文
202 20 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
Helldance 2010-08-05
  • 打赏
  • 举报
回复
servlet没有初始化。
加上init()方法
izard999 2010-05-27
  • 打赏
  • 举报
回复
现在有些忙,, 晚上再帮你找问题吧.!
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
help。。。。。。。。。。。
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
等待。。。。。。。。。
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 hello_amy 的回复:]
Test 类中没有id="sumbean"的setter方法
[/Quote]
不是Test类的问题。是Servlet内面获取不了applicationContext。
hello_Amy 2010-05-27
  • 打赏
  • 举报
回复
Test 类中没有id="sumbean"的setter方法
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 closewbq 的回复:]
cxt为空!
[/Quote]

就是要怎么样取到的ctx 才不空呢?????
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 izard999 的回复:]
这个空指针 是你根本就没拿到ctx的原因

XML code

<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</se……
[/Quote]
其中:
ServletContext sc= getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
PrintWriter out = response.getWriter();
out.print(ctx);
打印null
证明ctx 还是没有获取到??????????????????????????????
closewbq 2010-05-27
  • 打赏
  • 举报
回复
cxt为空!
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 izard999 的回复:]
这个空指针 是你根本就没拿到ctx的原因

XML code

<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</se……
[/Quote]

使用DispatcherServlet后如果修改contextConfiglocation 的bean文件名与实际的不一样的话会报错。
严重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/beanscannofind.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/beanscannofind.xml]
是不是就可以证明Spring的配置是已经加载的。
可是为什没getbean还是
java.lang.NullPointerException
mytest.SpringTestServlet.service(SpringTestServlet.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
严重: Servlet.service() for servlet SpringTestServlet threw exception
java.lang.NullPointerException
at mytest.SpringTestServlet.service(SpringTestServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

all the same
kaynezhang 2010-05-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 justwhat 的回复:]

另:发现即使改了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beanscannofound.xml</param-value>
</context-param>
而spring 配置的beans文件名不变,都不报file can ……
[/Quote]

改过来就重新发布一下,试试。
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
请问问什么要加上面这个配置。??? 立刻试试ing
izard999 2010-05-27
  • 打赏
  • 举报
回复
在web.xml中加上上面的servlet配置
izard999 2010-05-27
  • 打赏
  • 举报
回复
这个空指针 是你根本就没拿到ctx的原因

<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

这样配置看.!
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
另:发现即使改了
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beanscannofound.xml</param-value>
</context-param>
而spring 配置的beans文件名不变,都不报file can not found 错唔,应该是spring 没有加载吧?
可是
<listener>
<listener-class>org.springframework.web.context.ContextLoader</listener-class>
</listener>
spring 不就是已经会在tomcat 启动的时候自动加载了么?
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
<bean id="sumbean" class="mytest.Test"></bean>
</beans>

Test t = (Test)ctx.getBean("sumBean");

注意大小写

已经改过来。。还是一样。
kaynezhang 2010-05-27
  • 打赏
  • 举报
回复

<bean id="sumbean" class="mytest.Test"></bean>
</beans>

Test t = (Test)ctx.getBean("sumBean");


注意大小写
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
觉得是Spring 容器是没有加载?但是:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoader</listener-class>
</listener>
不是已经可以加载了么? 求解。。。
JustWhat 2010-05-27
  • 打赏
  • 举报
回复
Test t = (Test)ctx.getBean("sumbean");//36行

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧