SpringServletContext简单案例

一、项目结构及相应jar包,如下图

二、UserService代码

package com.hjp.service;

/**
 * Created by JiaPeng on 2015/11/15.
 */
public class UserService {
    public void addUser() {
        System.out.println("add user");
    }
}

UserService

三、创建一个HelloServlet,代码:

package com.hjp.web.servlet;

import com.hjp.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;

/**
 * Created by JiaPeng on 2015/11/15.
 */
public class HelloServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        //方式1,手动从ServletContext作用域中获得内容
        //WebApplicationContext applicationContext = (WebApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        WebApplicationContext applicationContext= WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.addUser();
    }
}

HelloServlet

四、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_3_1.xsd"
         version="3.1">
    <!--设置web全局初始化参数,设置配置文件位置
        名称#contextConfigLocation 固定
        值#  "classpath:"表示类路径 (src)
            也可以有子包,写法#classpath:com/hjp/applicationContext.xml
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--配置监听器,用于加载spring配置文件-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.hjp.web.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/HelloServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

web.xml

五、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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">
    <bean id="userService" class="com.hjp.service.UserService"></bean>
</beans>

applicationContext.xml

时间: 2024-08-11 09:57:38

SpringServletContext简单案例的相关文章

keepalived简单案例

---------------------------------- 一.前言 二.环境 三.Keepalived体系架构 四.安装Keepalived 五.案例配置 ---------------------------------- 一.前言 keepalived是一个类似于layer3,4&5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的

[Design Pattern] Mediator Pattern 简单案例

Meditor Pattern,即调解模式,用一个调解类类处理所有的沟通事件,使得降低多对象之间的沟通难度,属于行为类的设计模式.为了方便理解记忆,我也称其为,沟通模式. 下面是一个调解模式的简单案例. ChatRoom 提供公用发送短信的方法.User 全部通过 ChatRoom 类发送信息进行沟通.MediatorPatternDemo 演示调解模式. 代码实现 ChatRoom 提供发送信息的方法 public class ChatRoom { public static void sho

[Design Pattern] Iterator Pattern 简单案例

Iterator Pattern,即迭代时模式,按照顺序依次遍历集合内的每一个元素,而不用了解集合的底层实现,属于行为类的设计模式.为了方便理解记忆,我也会称其为遍历模式. 下面是一个迭代器模式的简单案例. Iterator 定义了迭代接口, 仅 hasNext 和 next 两个方法.Container 定义了集合的接口,必须包含一个返回迭代器类 Iterator 的方法.NameRepository 是容器的具体类,实现 Container 接口,并拥有一个内部类 NameIterator

[Design Pattern] Observer Pattern 简单案例

Observer Pattern,即观察者模式,当存在一对多关系,例如一个对象一有变动,就要自动通知被依赖的全部对象得场景,属于行为类的设计模式. 下面是一个观察者模式的简单案例. Observer 定义观察者的接口,定义需要观察的对象,已经被通知的接口.BinaryObserver, OctalObserver, HexaObserver 各自实现了 Observer 接口. Subject 是被观察的对象,记录了观察该对象的观察者列表,自身有变动,即可通知观察者列表中的各个观察者. 代码实现

[Design Pattern] Command Pattern 简单案例

Command Pattern, 即命令模式,把一个命令包裹在一个对象里面,将命令对象传递给命令的执行方,属于行为类的设计模式 下面是命令模式的一个简单案例. Stock 代表被操作的对象.Order 定义命令的接口,BuyStock, SellStock 是具体类,实现 Order 接口.Broker 是命令的执行方.CommandPatternDemo 演示命令模式. 代码实现 Order, 命令接口 public interface Order { public void execute(

[Design Pattern] Proxy Pattern 简单案例

Proxy Pattern, 即代理模式,用一个类代表另一个类的功能,用于隐藏.解耦真正提供功能的类,属于结构类的设计模式. 下面是 代理模式的一个简单案例. Image 定义接口,RealImage, ProxyImage 都实现该接口.RealImage 具有真正显示功能,ProxyImage 作为代表,暴露给客户端使用. 代码实现: public interface Image { public void display(); } RealImage 的实现,提供真正的功能 public

[Design Pattern] Facde Pattern 简单案例

Facade Pattern, 即外观模式,用于隐藏复杂的系统内部逻辑,提供简洁的接口给客户端调用,属于结构类的设计模式.我会将其名字理解为,门户模式. 下面是 Facade Pattern 的一个简单案例. Shape 定义一个接口,Circle, rectangle, Square 分别实现 Shape 接口,代表系统内部的一个功能.ShapeMaker 作为一个对外类,提供简洁的接口给外部调用. 代码实现: Shape 接口 public interface Shape { public

[Design Pattern] Flywight Pattern 简单案例

Flywight Pattern, 即享元模式,用于减少对象的创建,降低内存的占用,属于结构类的设计模式.根据名字,我也将其会理解为 轻量模式. 下面是享元模式的一个简单案例. 享元模式,主要是重用已有的对象,通过修改部分属性重新使用,避免申请大量内存. 本模式需要主要两个点: 1. 对象的 key 应该是不可变得,本例中 color 作为 key,所以我在 color 前添加了 final 的修饰符. 2. 并非线程安全,多个线程同时获取一个对象,并同时修改其属性,会导致无法预计的结果. 代码

[源码]Condition的原理,简单案例(ArrayBlockingQueue),复杂案例(LinkedBlockingQueue).

源代码解析 Re'entrantLock lock = new ReentrantLock(fair); Condition   notEmpty = lock.newCondition(); //返回内部类 AbstractQueuedSyncronizer.ConditionObject 各自维护了两个队列.一个是阻塞同步队列 syncQueue 双向队列,一个是条件等待队列. Condition.await两个作用.1.放入同步队列 park 2.realse锁,3等待别人获取锁acqui