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容器的实例对象,即:
(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). 测试代码:
实体类:
Spring配置文件:
Servlet配置文件:
监听器:
测试类:
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(); } }
相关文章推荐
- Spring_34_在 WEB 应用中使用 Spring 的基本思路
- 在 WEB 应用中使用 Spring 的基本思路(原生)
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
- 使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
- spring-boot-web-ui及thymeleaf基本使用
- ThreadLocal的实现原理,及使用实例,解决spring,hibernate非web项目下的懒加载 no session or session was closed(2)!
- webwork+spring+ibatis整合使用
- CGIC简明教程1:使用CGIC的基本思路
- 关于JPA(Hibernate)+spring在未使用web的情况下使用懒加载,导致no sesssion or session has closed!
- web 项目中 在spring中使用log4j 定时刷新log4j配置文件
- 使用WebContainer中的Spring 常用方法
- 使用Spring.Net对Web页面进行依赖注入
- Spring2.5中使用方法注入(method inject)之基本方法替代(basis method replacement)
- Spring.Net的基本使用
- 此网页不允许使用基本类型“System.Web.UI.MasterPage”该类型未注册为安全类型
- web 环境,非web 环境中,freemarker 结合 spring使用。
- 使用Spring.Net对Web页面进行依赖注入