您的位置:首页 > 编程语言 > Java开发

Spring在Web中使用的基本思路

2014-11-30 17:05 344 查看
1. 加入额外的jar包:spring-web-4.0.0.RELEASE和spring-webmvc-4.0.0.RELEASE。

2. Spring的配置文件没有什么不同之处,按常规配置即可。

3. 如何创建IOC容器:

(1). 在非Web应用中,我们直接在main()中创建IOC容器的实例对象,即:

public static void main(String[] args) {
//1.创建Spring的IOC容器对象,在该过程中会调用实体类的构造器和set方法
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
//2.从IOC容器中获取实例对象
HelloWorld helloWorld = (HelloWorld) act.getBean("helloworld");
//3.调用实例方法
helloWorld.hello();
}


(2). 在Web应用中如何创建呢?在Web应用被服务器加载时就创建IOC容器的实例。

此时可以利用监听器,我们知道监听器是Servlet规范中定义的一种特殊的类,它主要是对ServletContext、Session和Request的监听。所以

当监听器监听ServletContext的时候,可以在ServletContextListener中的contextInitialized(ServletContextEvent sce)中创建IOC容器。

(3). Web应用中的其他组件如何来访问IOC容器呢?

当在ServletContextListener中的contextInitialized(ServletContextEvent sce)中创建IOC容器以后,将IOC实例存放到

ServletContext的一个属性中(即application域中)。

(4). 在开发中为了增加应用的灵活性,可以将Spring配置文件的名字和位置配置到当前应用的初始化参数当中比较好,即在web.xml文件中配置。

(5). 测试代码:


实体类:

package bean;

public class HelloWorld {
public void hello() {
System.out.println("Hello,My name is Kate.");
}
}


Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="helloWorld" class="bean.HelloWorld"></bean>
</beans>


Servlet配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
<!-- 表示application域中的参数 -->
<context-param>
<param-name>configLocation</param-name>
<param-value>bean.xml</param-value>
</context-param>

<!-- 配置监听器 -->
<listener>
<listener-class>listener.IOCListener</listener-class>
</listener>

<servlet>
<servlet-name>TestSpringServlet</servlet-name>
<servlet-class>servlet.TestSpringServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>TestSpringServlet</servlet-name>
<url-pattern>/testSpring</url-pattern>
</servlet-mapping>
</web-app>


监听器:

package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class IOCListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//1.获取Spring配置文件的名称
ServletContext servletContext = sce.getServletContext();
String config = servletContext.getInitParameter("configLocation");
//2.创建IOC容器
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
//3.把IOC容器实例存放到ServletContext的属性中
servletContext.setAttribute("applicationContext", ac);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("IOCListener destroyed...");
}
}


测试类:

package servlet;

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 bean.HelloWorld;

public class TestSpringServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//获取IOC容器实例
ApplicationContext ac = (ApplicationContext) this.getServletContext()
.getAttribute("applicationContext");
System.out.println(ac);
HelloWorld hello = (HelloWorld) ac.getBean("helloWorld");
hello.hello();
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: