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环境(手动实现)的更多相关文章

  1. Spring集成web环境(使用封装好的工具)

    接上文spring集成web环境(手动实现) ##########代码接上文############# spring提供了一个监听器ContextLoaderListener对上述功能的封装,该监听器 ...

  2. Spring与Web环境集成

    1. Spring与Web环境集成 1.1 ApplicationContext应用上下文获取方式 应用上下文对象是通过new ClasspathXmlApplicationContext(sprin ...

  3. Spring(五)Spring与Web环境集成

    MVC 是 Model.View 和 Controller 的缩写,分别代表 Web 应用程序中的 3 种职责. 模型:用于存储数据以及处理用户请求的业务逻辑. 视图:向控制器提交数据,显示模型中的数 ...

  4. Shiro集成web环境[Springboot]-基础使用

    Shiro集成web环境[Springboot] 1.shiro官网查找依赖的jar,其中shiro-ehcache做授权缓存时使用,另外还需要导入ehcache的jar包 <dependenc ...

  5. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

  6. spring集成环境下的axis webservice的发布,调试

    在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...

  7. Web环境中Spring的启动过程

    1.spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置: …… <context-par ...

  8. Spring-IOC 在非 web 环境下优雅关闭容器

    当我们设计一个程序时,依赖了Spring容器,然而并不需要spring的web环境时(Spring web环境已经提供了优雅关闭),即程序启动只需要启动Spring ApplicationContex ...

  9. Shiro在Web环境下集成Spring的大致工作流程

    1,Shiro提供了对Web环境的支持,其通过一个 ShiroFilter 入口来拦截需要安全控制的URL,然后进行相应的控制.      ①配置的 ShiroFilter 实现类为:org.spri ...

随机推荐

  1. Vue3+ElementPlus的一套开源前台+后台管理模板

    Vue-Element-Faster 基于最新版vue.js 3.x与element-plus2.x实现的一套前台展示系统.后台管理系统模板,自带权限管理模块. API接口基于SpringBoot2. ...

  2. VBS文件无限循环解决办法

    VBS文件无限循环解决办法,也就相当于编程中的停止运行指令. 那么如何关掉VBS文件呢?当然关机后会自动关掉,还有另外一种方法就是,在"任务管理器"中找到进程"WScri ...

  3. Centos7 环境下设置固定IP

    1. 在/etc/sysconfig/network-scripts/下创建ifcfg-eth0配置文件, 并填入以下内容: DEVICE=eth0 TYPE=Ethernet IPADDR=192. ...

  4. (leetcode)二叉树的前序遍历-c语言实现

    给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 前序遍历 前序遍历首先 ...

  5. Redis 系统学习目录

    Redis 系统学习目录 1.redis是什么2.redis的作者何许人也3.谁在使用redis4.学会安装redis5.学会启动redis6.使用redis客户端7.redis数据结构 – 简介8. ...

  6. 【转】pringMVC+Hibernate+Spring 简单的一个整合实例

    ref:http://langgufu.iteye.com/blog/2088355 SpringMVC又一个漂亮的web框架,他与Struts2并驾齐驱,Struts出世早而占据了一定优势,我在博客 ...

  7. kafka端口和zookeeper端口

    一.问题描述 今天配合现场联调一个数据工具,工具使用到了kafka,程序启动之后包如下错误: [WARN ] [2020-08-17 19:17:27] [org.apache.kafka.clien ...

  8. 用TLS/SSL保证EMQ的网络传输安全

    作为基于现代密码学公钥算法的安全协议,TLS/SSL能在计算机通讯网络上保证传输安全,EMQ的MQTT broker支持TLS,也可以用这种方式来确保传输安全. 参考官网:https://www.em ...

  9. SpringDataJdbc整合MyBatis方式

    由于官方文档springdatajdbc整合mybatis过于简述,导致死磕了一段时间, SpringDataJdbc整合Mybatis的官方文档:https://docs.spring.io/spr ...

  10. 怎么理解 Redis 事务?

    1)事务是一个单独的隔离操作:事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的命令请求所打断. 2)事务是一个原子操作:事务中的命令要么全部被执行,要么全部都不执 ...