目的:记录spring整合 servlet过程demo。(企业实际开发中可能很少用到),融会贯通。

前言:在学习spring 过程(核心 ioc,aop,插一句 学了spring 才对这个有深刻概念, 在net时候都是直接使用,不得不说 java 还是深刻点)过程中,我们基本上都是在test中测试如图

环境:IDEA

但是,开发中是 spring容器 是共享的,其他地方直接调用,这里就涉及到spring和其他的整合,此文servlet 为测试点。

1:新建servlet 过程参考,https://www.cnblogs.com/y112102/p/11338610.html

2:导入jar包。(4个核心,一个依赖)后期开发推荐使用 Maven,后面在测试,示logging找不到  , 日志也要导入

   2.1: spring-web 也要记得

在复习一遍:

1:    基础:4+1 , beans、core、context、expression , commons-logging
2: AOP:aop联盟、spring aop 、aspect规范、spring aspect
3: db:jdbc、tx(事务)
4: 测试:test
5: web开发:spring web

3:web.xml 配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"> <!-- 确定配置文件位置:
classpath: src 或者classes 目录下-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置spring 监听器,加载xml配置文件
目的:tomcat启动的时候会自动根据配置文件创建 spring对象-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet> <!-- 浏览器访问 hello 找到HelloServlet 请求 -->
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping> </web-app>

3.2 参考3.1配置文件 新建 HelloServlet 不要过多解释,如图:

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
/**
* Create by on 2019-09-10
*
* @author lsw
*/
public class HelloServlet extends HttpServlet { @Override
public void doGet(HttpServletRequest request, HttpServletResponse response){ try { // 从application作用域(ServletContext)获得spring容器
//方式1: 手动从作用域获取 getAttribute返回的Object类型 需要强转
ApplicationContext applicationContext =
(ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); //方式2:通过工具获取
ApplicationContext apppApplicationContext2 =
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); //从容器中取Bean的实例
var c = applicationContext.getBean("AddressId", Address.class);
System.out.println( c.toString() ); response.getWriter().println("<h1>Hello Servlet!</h1>");
} catch (IOException e) {
e.printStackTrace();
}
}
}

最后启动运行结果:

控制台:

总结:

servlet :web层,这里将来是spring mvc ,有很多选择 比如 struts(现在也很少用到了)

mybatis:dao层,知识点杂

spring:service层

java 入门 坚持下,你会发现很简单的,特别是net开发者,接触java 后才去理解net的开发方式 又会有另一番心得。

所有的都是围绕次服务 ,扩展的。

spring 整合 servlet的更多相关文章

  1. spring boot整合servlet、filter、Listener等组件方式

    创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...

  2. spring boot 2.x 系列 —— spring boot 整合 servlet 3.0

    文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...

  3. Spring Boot整合Servlet,Filter,Listener,访问静态资源

    目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...

  4. Spring Boot 整合Web 层技术(整合Servlet)

    1 整合Servlet 方式一1.1通过注解扫描完成Servlet 组件的注册      1.1.1创建Servlet /*** 整合Servlet 方式一*/@WebServlet(name = & ...

  5. 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)

    在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...

  6. Spring Boot整合Servlet、Filter、Listener

    整合 Servlet   方式一:   编写 servlet package com.bjsxt.controller; import javax.servlet.ServletException; ...

  7. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  8. Spring整合web开发

    正常整合Servlet和Spring没有问题的 public class UserServlet extends HttpServlet { public void doGet(HttpServlet ...

  9. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

随机推荐

  1. pandas-14 concatenate和combine_first的用法

    pandas-14 concatenate和combine_first的用法 concatenate主要作用是拼接series和dataframe的数据. combine_first可以做来填充数据. ...

  2. php长连接应用

    php长连接和短连接 2012-12-05 17:25 3529人阅读 评论(0) 收藏 举报  分类: 我的收藏(8)  什么是长连接,如果你没听说过,可以往下看! 长连接到底有什么用?我想你应该见 ...

  3. vector-空间增长

    使用 vector 的时候,一般是从一个空 vector 开始,根据需要逐步填充数据. 这里的关键惭怍是 push_back(),它将一个新元素添加到 vector 中,该元素成为 vector 的最 ...

  4. 供应链管理如何提高效率?APS系统成优化引擎

    APS系统,虽然它的起兴只有短短的十几年,但是在这段时间里面,它为很多企业解决了很多人工手动.脑力不可解决的问题. 所以APS被誉为供应链优化引擎,APS常常被称为高级计划与排程,但也有称为高级计划系 ...

  5. Building Objective-C static libraries with categories

    Q: How do I fix "selector not recognized" runtime exceptions when trying to use category m ...

  6. 【TTS】传输表空间Linux asm -> AIX asm

    [TTS]传输表空间Linux asm -> AIX asm 一.1  BLOG文档结构图       一.2  前言部分   一.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌 ...

  7. 图记 2016.1.7 获取本地图片、Bitmap转image

    这几天完成的内容有: 1.“添加图片”按钮 2.添加图片功能 遇到的问题: 我想要将添加图片按钮放在右下角,所以采用了相对布局,但是问题随之二来,因为将导航栏设置成了半透明,所以图片放到右下角之后,半 ...

  8. Linux LVM 逻辑卷管理

    使用Linux好久了,一定会意识到一个问题,某个分区容量不够用了,想要扩容怎么办?这里就涉及到LVM逻辑卷的管理了,可以动态调整Linux分区容量. LVM 概述 全称Logical Volume M ...

  9. Spring Boot 默认支持的并发量

    Spring Boot应用支持的最大并发量是多少? Spring Boot 能支持的最大并发量主要看其对Tomcat的设置,可以在配置文件中对其进行更改.当在配置文件中敲出max后提示值就是它的默认值 ...

  10. MySQL/MariaDB数据库的存储过程

    MySQL/MariaDB数据库的存储过程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.存储过程概述 1>.存储过程优势 存储过程把经常使用的SQL语句或业务逻辑封装起 ...