spring 整合 servlet
目的:记录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的更多相关文章
- spring boot整合servlet、filter、Listener等组件方式
创建一个maven项目,然后此项目继承一个父项目:org.springframework.boot 1.创建一个maven项目: 2.点击next后配置父项目及版本号 3.点击finish后就可查看p ...
- spring boot 2.x 系列 —— spring boot 整合 servlet 3.0
文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...
- Spring Boot整合Servlet,Filter,Listener,访问静态资源
目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...
- Spring Boot 整合Web 层技术(整合Servlet)
1 整合Servlet 方式一1.1通过注解扫描完成Servlet 组件的注册 1.1.1创建Servlet /*** 整合Servlet 方式一*/@WebServlet(name = & ...
- 从零开始的Spring Boot(2、在Spring Boot中整合Servlet、Filter、Listener的方式)
在Spring Boot中整合Servlet.Filter.Listener的方式 写在前面 从零开始的Spring Boot(1.搭建一个Spring Boot项目Hello World):http ...
- Spring Boot整合Servlet、Filter、Listener
整合 Servlet 方式一: 编写 servlet package com.bjsxt.controller; import javax.servlet.ServletException; ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
- Spring整合web开发
正常整合Servlet和Spring没有问题的 public class UserServlet extends HttpServlet { public void doGet(HttpServlet ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
随机推荐
- nodeJS从入门到进阶二(网络部分)
一.网络服务器 1.http状态码 1xx: 表示普通请求,没有特殊含义 2xx:请求成功 200:请求成功 3xx:表示重定向 301 永久重定向 302 临时重定向 303 使用缓存(服务器没有更 ...
- CentOS 下运行.net Core程序
系统: 阿里云的默认 CentOS 7.5 镜像 项目环境:.Net Core 2.2 一.安装.Net Core的运行环境 第一步,如果是一台新的服务器,可以升级一下系统的基础软件.如果没有必要也可 ...
- 珠宝juelrye宝石
juelrye n.珠宝 late 14c., juelrye "precious ornaments, jewel juelrye (uncountable) Adornment with ...
- Unity Physicals Rigidbody with multiple colliders
Rigidbody with multiple colliders adding colliders changes the center of mass and rotation behaviour ...
- ffmpeg 把视频转换为图片
ffmpeg -i "Tail of Hope.mp4" -r 1 -q:v 2 -f image2 pic-%03d.jpeg
- 小米8seroot后更改hosts文件记录
1.先备份原有文件,在设置中有个备份与恢复,可以备份,备份后链接电脑,传电脑上 2.下载开发版miui,安装 3.解bl锁,先到官网申请,按照提示操作. 4.备份文件传到手机,恢复文件 5.获得roo ...
- win7彻底卸载VS2015
C盘越来越臃肿,VS2013.VS2015.VS2017以及VS2019都安装的情况下,C盘没有空间了. 系统:Win7 卸载办法: 管理员方式进cmd,执行vs_professional.exe / ...
- linux设备驱动程序-i2c(1):i2c总线的添加与实现
linux设备驱动程序-i2c(1):i2c总线的添加与实现 (基于4.14内核版本) 在上一章节linux设备驱动程序-i2c(0)-i2c设备驱动源码实现中,我们演示了i2c设备驱动程序的源码实现 ...
- Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...
- python Thread 函数
构造方法: Thread(group=None, target=None, name=None, args=(), kwargs={}) group: 线程组,目前还没有实现,库引用中提示必须是No ...