spring在WEB中的应用。
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中的应用。的更多相关文章
- Spring 在Web中的应用
Spring 在Web中的应用 在web项目开发中,不会直接实例化ApplicationContext对象,如果想用到ApplicationContext,一般的步骤: 配置一个监听器ContextL ...
- 如何在Spring异步调用中传递上下文
以下文章来源于aoho求索 ,作者aoho 1. 什么是异步调用? 异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等待上一步 ...
- J2EE进阶(五)Spring在web.xml中的配置
J2EE进阶(五)Spring在web.xml中的配置 前言 在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制自动加载到容器中.在web ...
- 使用Spring时web.xml中的配置
使用Spring时web.xml中的配置: <?xml version="1.0" encoding="UTF-8"?> <web-app x ...
- spring web中的filter
昨天看了会spring web中部分代码,主要是各种filter,回顾一下: Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所 ...
- 重新学习Spring一--Spring在web项目中的启动过程
1 Spring 在web项目中的启动过程 Spring简介 Spring 最简单的功能就是创建对象和管理这些对象间的依赖关系,实现高内聚.低耦合.(高内聚:相关性很强的代码组成,既单一责任原则:低耦 ...
- 转载:如何让spring mvc web应用启动时就执行
转载:如何让spring mvc web应用启动时就执行特定处理 http://www.cnblogs.com/yjmyzz/p/4747251.html# Spring-MVC的应用中 一.Appl ...
- 第一个Spring Boot Web程序
需要的环境和工具: 1.Eclipse2.Java环境(JDK 1.7或以上版本)3.Maven 3.0+(Eclipse已经内置了) 写个Hello Spring: 1.新建一个Maven项目,项目 ...
- 在Spring的bean中注入HttpServletRequest解密
我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这 ...
随机推荐
- 【HDU6662】Acesrc and Travel【树形DP】
题目大意:给你一棵树,每个节点有一个权值,Alice和Bob进行博弈,起点由Alice确定,确定后交替选择下一个点,Alice目标是最终值尽可能大,Bob目标是尽可能小 题解:很明显是树形DP,那么考 ...
- Linux配置postfix
启动报错:主机名不能以数字开头,否则报错
- spring+cxf
里面有http://127.0.0.1:8081/dcs/soap/cls http://127.0.0.1:8081/dcs/soap/cms http://127.0.0. ...
- 富文本编辑器——百度UEditor插件安装教程
一.使用环境 Win7 Eclipse jettty9 chrome 二.下载百度UEditor插件 1.下载地址:http://ueditor.baidu.com/website/download. ...
- ASP.NET免费发送邮件|
因为之前有做过邮件发送的项目,最近也看一些朋友问起这个的做法,现在拿来给大家查看下.因为那时候是公司的服务器配置的.所以后来自己便在网上找到了一个可以任何个人都是可以使用的邮件发送.小弟新手,高手看到 ...
- php面试专题---2、常量及数据类型考点
php面试专题---2.常量及数据类型考点 一.总结 一句话总结: 变量为null和变量判断为false的情况需要仔细注意下 1.PHP中字符串可以使用哪三种定义方法以及各自的区别是什么? 单引号:不 ...
- 单例模式@Singleton在测试中的运用
前言 单例模式是一种比较常用的设计模式,目的是:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 测试种可能用到的场景 : 在很多时候,有些对象我们希望在整个程序只有一个实例,如线程池.数据库连 ...
- English-Words with 'ir'
hire thirty thirteen third sir birthday shirt stir circle dirty skirt affirm affirmation affirmable ...
- js 判断对象的长度
Object.size = function(obj) { var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) siz ...
- spring 注释
4