搭建一个从监听器函数启动的Sping工程,本文仅使用Spring的IoC来为我们管理bean。
在一个简单的Servlet中获取Spring托管的Bean对象,来验证Spring是否正常启动。
一、servlet service函数代码
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
System.out.println("service.");
ServletContext sc = request.getServletContext();
//获取Spring上下文
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
//从Spring上下文中获取Bean实例
Student student = context.getBean(Student.class);
System.out.println(student);
}
请求该Servlet两次,程序输出:
init.
service.
com.baymax.study_spring_2.model.Student@2e9981bf
service.
com.baymax.study_spring_2.model.Student@2e9981bf
二、Bean对象
@Component
public class Student {
private String name;
private int age;
private String classes;
...
三、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Empower System</display-name>
<!-- 首页配置 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 上下文参数配置 -->
<!-- Spring配置文件加载地址 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!-- Servlet配置 -->
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.baymax.study_spring_2.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<!-- 监听器配置 -->
<!-- Spring context加载监听,Spring程序就是从这启动的 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
四、pom.xml中Spring所需的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- 如果要从监听器启动Spring,必须要此依赖,因为ContextLoaderListener就在这个包中 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
</dependencies>
五、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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-2.5.xsd
">
<!-- **********************spring config start********************** -->
<!-- 隐式注入spring的注解处理类 -->
<context:annotation-config />
<!-- 配置组件自动扫描装配路径及筛选条件 -->
<context:component-scan base-package="com.baymax.study_spring_2"/>
<!-- **********************spring config end********************** -->
</beans>