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 ...
随机推荐
- kali之nmap
nmap简介 Nmap,也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包.可以扫描主机.端口.并且识别端口所对应的协议,以及猜测操作系统 Ping扫描(-sP参数) TCP ...
- SpringBoot启动原理详解
SpringBoot和Spring相比,有着不少优势,比如自动配置,jar直接运行等等.那么SpringBoot到底是怎么启动的呢? 下面是SpringBoot启动的入口: @SpringBootAp ...
- 连续子数组的最大乘积及连续子数组的最大和(Java)
1. 子数组的最大和 输入一个整形数组,数组里有正数也有负数.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.例如数组:arr[]={1, 2, 3, -2, ...
- 网络编程之 tcp服务器(一)
1.创建套接字 2.bind绑定ip和port 作为服务方,ip port 应该是固定的,所以要绑定;客户端一般不绑定 3.listen使套接字变成监听套接字,即变为被动链接 4.accept等待客户 ...
- Integer装箱拆箱、参数传递
拆箱装箱 举个例子 @Test public void testEquals() { int int1 = 12; int int2 = 12; Integer integer1 = new Inte ...
- WPF应用打包流程
1,安装工程模板插件Microsoft Visual Studio Installer Projects https://marketplace.visualstudio.com/items?item ...
- 一行Python代码画心型
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yanlaifan/article/deta ...
- Shell脚本编程30分钟入门(小结)(转)
原文链接https://www.jb51.net/article/161028.htm 什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿.程序化脚本,是一种 ...
- ELK Stack部署
部署ELK Stack 官网:https://www.elastic.co 环境准备: ip hostname 服务 用户.组 192.168.20.3 node2003 kibana6.5,file ...
- 日常工具集和技巧分享(Linux向)
好用的软件/终端 命令别名 ip切换 imgcat 终端查看图片 autojump 目录快捷跳转 titan 密码记录工具 tldr man 的简单版,在线查询linux命令示例 ssh 别名/免密码 ...