- Spring如何在WEB应用中使用?
- spring-web-4.0.0.RELEASE.jar
- spring-webmvc-4.0.0.RELEASE.jar
- Spring的配置文件,没有什么不同
- 如何创建IOC容器
- 非WEB应用在main方法中直接创建
- 应该在WEB应用被服务器加载时就创建IOC容器:
- 在ServletContextListener#contextInitialized(ServletContextEvent sce) 方法中创建IOC容器。
- 在WEB应用的其他组件中如何来访问IOC容器呢?
- 在ServletContextListener#contextInitialized(ServletContextEvent sce)方法中创建IOC容器后,可以把其放在ServletContext(即application域)的一个属性中。
- 实际上Spring配置文件的名字和位置应该也是可配置的!将其配置到当前WEB应用的初始化参数中较为合适。
例子程序
基本结构
Person.java
package com.spring.struts2.beans;
public class Person {
private String username;
public void setUsername(String username) {
this.username = username;
}
public void hello(){
System.out.println("My name is "+username);
}
}
SpringServletContextListener.java
package com.spring.struts2.listeners;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SpringServletContextListener implements ServletContextListener{
// Public constructor is required by servlet spec
public SpringServletContextListener() {
}
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent arg0) {
//1.获取Spring配置文件的名称。
ServletContext servletContext = arg0.getServletContext();
String config = servletContext.getInitParameter("configLocation");
//1.创建IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
//2.把IOC容器放在ServletContext的一个属性中
servletContext.setAttribute("ApplicationContext",ctx);
}
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
}
}
TestServlet.java
package com.spring.struts2.servlets;
import com.spring.struts2.beans.Person;
import org.springframework.context.ApplicationContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.从application域对象中得到IOC容器的利用
ServletContext servletContext = getServletContext();
ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
//2.从IOC容器中得到需要的bean
Person person = ctx.getBean(Person.class);
person.hello();
}
}
applicationContext.xml
<?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="person"
class="com.spring.struts2.beans.Person">
<property name="username" value="spring"></property>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置Spring配置文件的名称和位置-->
<context-param>
<param-name>configLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<!--启动IOC容器的ServletContextListener-->
<listener>
<listener-class>com.spring.struts2.listeners.SpringServletContextListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.spring.struts2.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%--
Created by IntelliJ IDEA.
User: 14741
Date: 2018/12/9
Time: 15:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/TestServlet">TestServlet</a>
</body>
</html>