配合sevlet的监听器实现。
一、spring在web应用中使用和spring在非web应用中使用有什么区别?
(1)jar包不一样
需要额外导入spring web相关的包:
- spring-web-4.2.5.RELEASE.jar
- spring-webmvc-4.2.5.RELEASE.jar
(2)spring的配置文件
并没有什么不同。
(3)如何创建ioc容器
1.非web应用在main方法中直接创建
2.web应用应该在web应用被服务器加载时就创建ioc容器
不能再web应用已经运行了再来创建。因为运行的时候可能已经需要使用ioc容器了,这个时候再来创建,效率低,速度慢。
那么我们怎么知道服务器什么时候加载呢?
在ServletContextListener的contextInitialized(ServletContextEvent arg0)方法中实现ioc容器,那么在web应用被启动的时候,就可以立即使用ioc容器了。
3.在web应用的其他组件中如何访问ioc容器
在创建了ioc容器后,可以把容器对象放在servlet context(即application域)的一个属性中
4.spring配置文件的名字和位置应该也是可配置的
将其配置到当前web应用的初始化参数中较为合适。
二、具体实现
项目结构:
(1)导入以下包
- commons-logging-1.2.jar
- spring-aop-4.2.5.RELEASE.jar
- spring-aspects-4.2.5.RELEASE.jar
- spring-beans-4.2.5.RELEASE.jar
- spring-context-4.2.5.RELEASE.jar
- spring-core-4.2.5.RELEASE.jar
- spring-expression-4.2.5.RELEASE.jar
- spring-jdbc-4.2.5.RELEASE.jar
- spring-orm-4.2.5.RELEASE.jar
- spring-test-4.2.5.RELEASE.jar
- spring-tx-4.2.5.RELEASE.jar
- spring-web-4.2.5.RELEASE.jar
- spring-webmvc-4.2.5.RELEASE.jar
- spring-websocket-4.2.5.RELEASE.jar
(2)写个listener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Application Lifecycle Listener implementation class listener * */ @WebListener public class listener implements ServletContextListener { /** * Default constructor. */ public listener() { // TODO Auto-generated constructor stub } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent servletContextEvent) { System.out.println("ioc create success"); // 获取spring配置文件的名称 ServletContext servletContext = servletContextEvent.getServletContext(); String config = servletContext.getInitParameter("configlocation"); // 创建ioc容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(config); servletContext.setAttribute("application", applicationContext); } } |
(2)写个实体类bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package bean; public class person { private String username; public void setUsername(String username) { this.username = username; } public void hello(){ System.out.println("my name is " + username); } } |
(3)写个servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; 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.person; public class servlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1.从application域对象中得到ioc容器的引用 ServletContext servletContext = getServletContext(); ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("application"); // 2.从ioc容器中得到需要的bean person person = (bean.person) applicationContext.getBean("person"); person.hello(); PrintWriter pw = resp.getWriter(); pw.print(66666); } } |
(4)配置spring配置文件
1 2 3 |
<bean id="person" class="bean.person"> <property name="username" value="godlikexie"></property> </bean> |
(5)配置web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<servlet> <servlet-name>servlet</servlet-name> <servlet-class>servlet.servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> <!-- 配置spring文件的名称 --> <context-param> <param-name>configlocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <!-- 配置spring的listener --> <listener> <listener-class>listener.listener</listener-class> </listener> |
(6)写个实现页面
1 2 3 4 5 6 7 8 9 10 11 12 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>test</title> </head> <body> <a href="servlet">testservlet</a> </body> </html> |
访问效果:
可以看见访问时ioc容器自动配置并且返回了结果。
这是spring在网络应用的基本思路之一。
三、总结
记录一下。