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. 关于mysql8.0 caching_sha2_password和sha256_password认证方式

    今天开发上线新系统反馈数据库连接有问题.自己分别在命令行下及navicat进行连接发现,发现root用户密码在命令行下可以正常连接,但是新建立的子用户连接不上.于是就换成管理员密码.立即就连接上了.看 ...

  2. java高并发之ConcurrentSkipListMap的那些事

    注意:本文内容基于JDK11,不同版本会有差异 ConcurrentSkipListMap的结构 ConcurrentSkipListMap是以链表(自然排序)的形式进行数据存储的.即在类中通过定义N ...

  3. CF809E题解

    给定一个排列 \(a_i\) 和一棵树,求: \[\frac 1 {n(n-1)}\sum_{i=1}^n\sum_{j=1}^n \varphi(a_i \times a_j) \times dis ...

  4. 写博客的技巧整理——基于Markdown

    我们需要掌握各种技巧,这样才能在写博客时游刃有余,以下内容觉得不错就点个赞吧 文章目录 1.目录与目录跳转 目录一(示例用勿点) 目录二(示例用勿点) 目录三(示例用勿点) 2.文字与图片 3.引用 ...

  5. 机器学习实战 | SKLearn最全应用指南

    作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/41 本文地址:http://www.showmeai.tech/article-det ...

  6. Java基础——引用类型作为形参与返回值

    一.具体类名作为形参与返回值 1.方法的形参是类名,其实需要的是该类的对象 比如有一个具体的猫类,我使用另一个类使用猫类的方法 public class CatOperator {   public ...

  7. Mybatis——一级缓存与二级缓存

    关于Mybatis的学习主要参考了狂神的视频 一级缓存 (1).使用范围:从sqlSession会话开始到结束 (2).使用:默认打开,无法关闭 (3).测试使用(需要打开日志观察数据库的连接情况): ...

  8. source insight新建工程

    1.打开Source Insight.如果已经打开过项目,则选择Project->Close Project.然后点击Project->New Project: 2.在弹出界面填入项目名 ...

  9. 6月28日 Django form组件 和 modelform组件

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  10. Spring核心思想:IOC(控制反转)、DI(依赖注入)和AOP(面向切面编程)

    Spring有三大核心思想,分别是控制反转(IOC,Inversion Of Controller),依赖注入(DI,Dependency Injection)和面向切面编程(AOP,Aspect O ...