1:创建IOC容器。在WEB应用程序启动的时候就创建。利用到监听器。

ServletContextListener类的contextInitialized方法中

 package com.struts2.listeners;

 import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringServletContextListener implements ServletContextListener{ public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub } public void contextInitialized(ServletContextEvent arg0) {
//1:applicationContext.xml在web.xml中进行创建。然后利用ServletContext获取到。
ServletContext sc=arg0.getServletContext();
String config=sc.getInitParameter("configLocation");
//创建IOC容器
ApplicationContext act=new ClassPathXmlApplicationContext(config);
//把创建的IOC容器放到ServletContext(即application域)中
sc.setAttribute("ApplicationContext", act);
} }

在web.xml中创建监听器和applicationContext.xml

<context-param>
<param-name>configLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>com.struts2.listeners.SpringServletContextListener</listener-class>
</listener>

然后创建一个实体:Person

package com.struts2.entyties;

public class Person {

    private String username;

    public void setUsername(String username) {
this.username = username;
} public void hello(){
System.out.println("My name is " + username);
} }

然后创建bean

    <bean id="person" class="com.struts2.entyties.Person">
<property name="username" value="陆伟"></property>
</bean>

然后写个servlet去使用:

package com.struts2.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import com.struts2.entyties.Person; public class TestServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 从 application 域对象中得到 IOC 容器的引用
ServletContext sc=getServletContext();
ApplicationContext act=(ApplicationContext) sc.getAttribute("ApplicationContext"); //2. 从 IOC 容器中得到需要的 bean
Person person = act.getBean(Person.class);
person.hello();
} }

在web.xml中加载servlet

<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.struts2.servlet.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>

然后写个页面进行访问:

<a href="TestServlet">TestServlet</a>

spring在WEB中的应用。的更多相关文章

  1. Spring 在Web中的应用

    Spring 在Web中的应用 在web项目开发中,不会直接实例化ApplicationContext对象,如果想用到ApplicationContext,一般的步骤: 配置一个监听器ContextL ...

  2. 如何在Spring异步调用中传递上下文

    以下文章来源于aoho求索 ,作者aoho 1. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步 ...

  3. J2EE进阶(五)Spring在web.xml中的配置

     J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...

  4. 使用Spring时web.xml中的配置

    使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...

  5. spring web中的filter

    昨天看了会spring web中部分代码,主要是各种filter,回顾一下: Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所 ...

  6. 重新学习Spring一--Spring在web项目中的启动过程

    1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...

  7. 转载:如何让spring mvc web应用启动时就执行

    转载:如何让spring mvc web应用启动时就执行特定处理 http://www.cnblogs.com/yjmyzz/p/4747251.html# Spring-MVC的应用中 一.Appl ...

  8. 第一个Spring Boot Web程序

    需要的环境和工具: 1.Eclipse2.Java环境(JDK 1.7或以上版本)3.Maven 3.0+(Eclipse已经内置了) 写个Hello Spring: 1.新建一个Maven项目,项目 ...

  9. 在Spring的bean中注入HttpServletRequest解密

    我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这 ...

随机推荐

  1. spring-boot 定时任务案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.Maven Plugin管理 pom.xml配置代码: <?xml versio ...

  2. JS中数据结构之列表

    列表是一组有序的数据.每个列表中的数据项称为元素.在 JavaScript 中,列表中的元素可以是任意数据类型.列表中可以保存多少元素并没有事先限定并可以不断壮大,实际使用时元素的数量受到程序内存的限 ...

  3. Codeforces 831C--Jury Marks (思维)

    题目链接:http://codeforces.com/problemset/problem/831/C 题意:有一位参赛选手,我们不知道他初始或最后的成绩,但是知道k次评审所加(减)的分数,以及n个在 ...

  4. Visual Studio Code 做PHP开发

    Visual Studio Code 做PHP开发 1. 在Windows 10环境下安装PHP: 1. 下载自己中意的PHP版本:http://windows.php.net/download (我 ...

  5. NOIp 图论算法专题总结 (3):网络流 & 二分图 简明讲义

    系列索引: NOIp 图论算法专题总结 (1) NOIp 图论算法专题总结 (2) NOIp 图论算法专题总结 (3) 网络流 概念 1 容量网络(capacity network)是一个有向图,图的 ...

  6. QTP-创建一个word文件

    '创建word实例 Set oWordApp = CreateObject("Word.Application") oWordApp.Visible =True '添加一个word ...

  7. Openstack API 类型 & REST 风格

    目录 目录 Openstack 提供了三种操作方式 Web界面 CIL 指令行 RESTful API REST 风格 RESTFul风格的API设计 基于HTTP协议的RESTful API Ope ...

  8. python接口自动化测试三十五:用BeautifulReport生成报告

    GitHub传送门:https://github.com/TesterlifeRaymond/BeautifulReport 配置BeautifulReport 下载.解压并修改名字为Beautifu ...

  9. 测开之路二十三:python常用模块

    os模块 sys模块 hashlib shutil对文件和目录进行操作 random和随机相关 json

  10. anaconda 安装2个python环境 亲测

    本机环境: anaconda3,pyhon3.7.4 配置第2个python环境,安装python3.6 > conda create --name tensorflow python=3.6 ...