Spring IoC工程搭建-从监听器启动

搭建一个从监听器函数启动的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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值