我想做一个最简单的Spring2的Web示例程序,可是总是没法注入,helloSpring和name总是返回空指针,请大家看看。
----------------------------配置文件-------------------------------------
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <br=""> xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloSpringServlet" class="willishz.hellospring.HelloSpringServlet" scope="session">
<property name="helloSpring">
<ref local="helloSpring">
</property>
<property name="name">
<value>Spring</value>
</property>
</bean>
<bean id="helloSpring" class="willishz.hellospring.HelloSpringImpl" scope="session">
</bean>
</beans>
----------------------------------Servlet---------------------------------------
package willishz.hellospring;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloSpringServlet extends HttpServlet {
private IHelloSpring helloSpring;
private String name;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IHelloSpring hp = (IHelloSpring) ctx.getBean("helloSpring");
System.out.println(hp.sayHello(name));
response.getWriter().print(helloSpring.sayHello(name));
}
public IHelloSpring getHelloSpring() {
return helloSpring;
}
public void setHelloSpring(IHelloSpring helloSpring) {
this.helloSpring = helloSpring;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
----------------------------------接口类---------------------------------------
package willishz.hellospring;
public interface IHelloSpring {
public abstract String sayHello(String name);
}
----------------------------------实现类---------------------------------------
package willishz.hellospring;
public class HelloSpringImpl implements IHelloSpring {
public String sayHello(String name) {
return name + " 你好!";
}
}
----------------------------------web.xml---------------------------------------
<web-app>
<display-name>hellospring</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>helloSpringServlet</servlet-name>
<servlet-class>willishz.hellospring.HelloSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloSpringServlet</servlet-name>
<url-pattern>/sayhello</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
问题补充
如果用
//IHelloSpring hp = (IHelloSpring) ctx.getBean("helloSpring");
//System.out.println(hp.sayHello(name));
就能注入成功。
改成:
[code="java"]public class HelloSpringServlet extends HttpServlet {
private IHelloSpring helloSpring;
private String name = "Spring"; // name 是什么, 为什么会放在这?
private ApplicationContext ctx;
@Override
public void init() throws ServletException {
// 在这里拿Spring的配置文件,只拿一次.
wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
// 在这里将helloSpring取到
helloSpring = (IHelloSpring) ctx.getBean("helloSpring");
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
// 怎么可以每次响应都重新载入一次Spring配置文件呢?
// IHelloSpring hp = (IHelloSpring) ctx.getBean("helloSpring");
System.out.println(helloSpring.sayHello(name));
response.getWriter().print(helloSpring.sayHello(name));
} [/code]
1, 在请求HelloSpringServlet这个servlet的时候,它的生命周期是由tomcat来管理的, 而不是由spring来管理的, 也就是说请求的这个实例不是在spring配置中:
[code="java"] [/code]的这个实例.
2, 由于在web.xml中配置了ContextLoaderListener, 所以ApplicationContext可直接用WebApplicationContextUtils.getWebApplicationContext(request.getServletContext())这个方法来拿, 因为此时已经由ContextLoaderListener设置在ServletContext中
3, 如果楼主用ctx.getBean("helloSpringServlet")得到的HelloSpringServlet实中, 会发现这个实例中的helloSpringt和name被spring注入了!
4, 如果想使用spring和web结合, 最好了解一种MVC框架, Webwork, Structs, Spring MVC都可以!
willishz,不知道你是不是必须使用servlet中入spring容器中的Bean,其实用现成的web框架更好一些.
你如果想实现 在servlet中注入Srping的Bean也是可以做到的,按照你的做法是不行的,你配置的那个servlet是不受Sping管理的,所以你必须通过一个Servlet 的代理来实现这一点,具体的参照这个:
[color=blue]http://www.javatx.cn/clubPage.jsp?ccStyle=0&tID=1877&ccID=33[/color]
写得十分详细.