spring_(27)Spring_在 WEB 應用中使用 Spring 的基本思路md
阿新 • • 發佈:2018-12-24
- 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>