willishz 2008-08-09 18:46
浏览 197
已采纳

请教一个最简单的Spring2的示例程序没法注入的问题

我想做一个最简单的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));

就能注入成功。

  • 写回答

3条回答 默认 最新

  • lggegegmail 2008-08-09 20:43
    关注

    改成:
    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块