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;就可以了. 但是,为什么我们可以直接这 ...
随机推荐
- 学习笔记:Apache Kylin 概述
一.kylin解决了什么关键问题? Apache Kylin的初衷就是解决千亿.万亿条记录的秒级查询问题,其中的关键就是打破查询时间随着数据量呈线性增长的这一规律. 大数据OLAP,我们可以注意到两个 ...
- python使用qq邮箱向163邮箱发送邮件、附件
在生成html测试报告后 import smtplib,time from email.mime.text import MIMEText from email.mime.multipart impo ...
- java中对象转json,json转list,json转map
在IDEA中的springboot项目里写的一个测试例子,新建User类,四个字段,Long id , String password,String userName,int age; 以及带参构造, ...
- 10.18.1 linux文本编辑器vim
vi和vim的区别 编辑一个文本时,vi不会显示颜色,而vim会显示颜色,vi 有点类似windows记事本,简单,那么就是vim复杂编辑器,功能复杂,高亮,自动缩进(写shell/python脚本用 ...
- C# 简单软件有效期注册的实现
◆需求:公司一直以来对开发的产品都没有进行使用时间的控制,要么就是将日期限制写死在程序里面,每次都要编译新程序再发给客户,很不方便.于是公司让我写个模块,要求如下:1.无论哪个新开发的程序只要调用这个 ...
- CF 717A Festival Organization——斯特林数+递推求通项+扩域
题目:http://codeforces.com/contest/717/problem/A 是 BJOI2019 勘破神机 的弱化版. 令 \( g[i] \) 表示长为 i .以 1 结尾的方案数 ...
- DB-概念-数据库:数据库/Database
ylbtech-DB-概念-数据库:数据库/Database 数据库是以一定方式储存在一起.能与多个用户共享.具有尽可能小的冗余度.与应用程序彼此独立的数据集合,可视为电子化的文件柜——存储电子文件的 ...
- 网络命令-nc(一)
一直在linux环境下编程,但却没有用过nc命令,不过最近发现Netcat这个命令-nc,发现真的蛮强大的, 为了备忘,就写了这个博客吧,不求全,只求把自己觉得很有用的命令整理出来,这篇文章估计要长期 ...
- java多线程学习笔记(七)
volatile关键字 关键字volatile的主要作用是使变量在多个线程间可见. public class PrintString { private boolean isContinue = tr ...
- linux目录及文件命令学习
学习Linux 目录操作 1.pwd 可以看当前目录路径 2.cd change directory 改变目录,切换目录 cd / 进入跟目录 cd ..返回上级目录 cd 进入用户主目录 cd .. ...