Spring集成web环境(手动实现)
1.创建UserDao接口及其实现类UserDaoImpl(接口代码省略)
public class UserDaoImpl implements UserDao {
@Override
public void save() {
System.out.println("save running....");
}
}
2.创建UserService及其实现类UserDaoImpl(接口代码省略)
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save1() {
userDao.save();
}
}
3.将UserDaoImpl及其UserServiceImpl注入到spring容器当中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置Dao-->
<bean id="userDao" class="com.hao.dao.impl.UserDaoImpl"/>
<!-- 配置Service-->
<bean id="userService" class="com.hao.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
</beans>
4.编写UserServlet
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean(UserService.class);
service.save1();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
5.配置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_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.hao.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
6.启动tomcat服务器,在浏览器访问userServlet

我们思考这一段代码
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean(UserService.class);
service.save1();
}
当我们每次访问userServlet时,是不是都会创建一个spring容器,这样是不是造成了很大地问题,还有一个问题就是我们在用户访问userServlet时才创建spring容器,是不是会造成效率底下地问题,所以引入了监听器的概念,我们使用了范围最大的ServletContextListener监听器(用来监听ServletContext,ServletContext对象在服务器启动时就会创建,在初始化方法时就创建spring容器,提供访问效率;
创建普通类,让它实现ServletContextListener接口
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//将spring应用上下文对象存储到ServletContext域中
ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute("app",context);
System.out.println("spring容器创建完毕!");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
在web.xml中配置监听器
<listener>
<listener-class>com.hao.listener.ContextLoaderListener</listener-class>
</listener>
修改UserServlet类里面的代码
public class UserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
ServletContext servletContext = this.getServletContext();
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
UserService userService = app.getBean(UserService.class);
userService.save1();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
然后启动服务器,访问userServlet(spring容器的创建在服务器启动时就会创建
结果:

Spring集成web环境(手动实现)的更多相关文章
- Spring集成web环境(使用封装好的工具)
接上文spring集成web环境(手动实现) ##########代码接上文############# spring提供了一个监听器ContextLoaderListener对上述功能的封装,该监听器 ...
- Spring与Web环境集成
1. Spring与Web环境集成 1.1 ApplicationContext应用上下文获取方式 应用上下文对象是通过new ClasspathXmlApplicationContext(sprin ...
- Spring(五)Spring与Web环境集成
MVC 是 Model.View 和 Controller 的缩写,分别代表 Web 应用程序中的 3 种职责. 模型:用于存储数据以及处理用户请求的业务逻辑. 视图:向控制器提交数据,显示模型中的数 ...
- Shiro集成web环境[Springboot]-基础使用
Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...
- Shiro集成web环境[Springboot]-认证与授权
Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...
- spring集成环境下的axis webservice的发布,调试
在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...
- Web环境中Spring的启动过程
1.spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置: …… <context-par ...
- Spring-IOC 在非 web 环境下优雅关闭容器
当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContex ...
- Shiro在Web环境下集成Spring的大致工作流程
1,Shiro提供了对Web环境的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制. ①配置的 ShiroFilter 实现类为:org.spri ...
随机推荐
- 6月19日 python学习总结 Django之路由系统
Django之路由系统 Django的路由系统 Django 1.11版本 URLConf官方文档 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调 ...
- 74cms v4.2.1-v4.2.129-后台getshell漏洞复现
0x00 影响范围 v4.2.1-v4.2.129 0x01 环境搭建 1. 先去官网下载 骑士人才系统基础版(安装包) 2.将下载好的包进行安装 0x02 复现过程 当前版本v4.2.111 点加工 ...
- CF17E Palisection(manacher/回文树)
CF17E Palisection(manacher/回文树) Luogu 题解时间 直接正难则反改成求不相交的对数. manacher求出半径之后就可以差分搞出以某个位置为开头/结尾的回文串个数. ...
- 如何看待malloc产生内存碎片
上代码直接研究: int main() { int *heap_d; int *heap_e; int *heap_f; heap_d = (int *)malloc(10); heap_e = (i ...
- li 与 li 之间有空白间隔是什么原因引起的,有什么解决办法
li 与 li 之间有空白间隔是什么原因引起的,有什么解决办法 原因 浏览器会把inline元素间的空白字符(空格.换行.Tab等)渲染成一个空格.而为了美观,我们通常是一个 放在一行,这导致 换行后 ...
- Kafka 的设计架构你知道吗?
Producer :消息生产者,就是向 kafka broker 发消息的客户端. Consumer :消息消费者,向 kafka broker 取消息的客户端. Topic :可以理解为一个队列,一 ...
- Kafka 消费者是否可以消费指定分区消息?
Kafa consumer消费消息时,向broker发出fetch请求去消费特定分区的消息,consumer指定消息在日志中的偏移量(offset),就可以消费从这个位置开始的消息,customer拥 ...
- JDBC 中如何进行事务处理?
Connection 提供了事务处理的方法,通过调用 setAutoCommit(false)可以设置 手动提交事务:当事务完成后用 commit()显式提交事务:如果在事务处理过程中 发生异常则通过 ...
- Java 中 Semaphore 是什么?
Java 中的 Semaphore 是一种新的同步类,它是一个计数信号.从概念上讲,从 概念上讲,信号量维护了一个许可集合.如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可.每 ...
- solr服务的搭建
首先你需要一台已经搭建好的虚拟机,下面的步骤才可以执行 安装java 安装完Centos6.5的Base Server版会默认安装OpenJDK,首先需要删除OpenJDK 1.查看以前是不是安装了o ...