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 ...
随机推荐
- if __name__ == '__main__' 该如何理解
Python 中的 if __name__ == '__main__' 该如何理解 程序入口 对于很多编程语言来说,程序都必须要有一个入口,比如 C,C++,以及完全面向对象的编程语言 Java,C# ...
- Oracle数据库之查询
一.单表简单查询: 1. select * from scott.emp 2.去重: --去除重复记录 select distinct ssex from java0322; select disti ...
- 【转】java 环境变量:path与classpath区别
path指示java命令的路径,像javac.java.javaw等: classpath是javac编译器的一个环境变量,它的作用与import.package关键字有关,当你写下improt ja ...
- I2C总线
PHILIPS公司开发的两线式串行总线 GPIO模拟i2c驱动中有自己的一套传输算法.GPIO模拟I2C是要占用CPU资源的,而用I2C芯片是不占CPU资源的 特点 接口线少,控制方式简单,器件封装形 ...
- oracle执行计划(一)----概述
(1)什么是执行计划SQL是一种傻瓜式语言,每一个条件就是一个需求,访问的顺序不同就形成了不同的执行计划.Oracle必须做出选择,一次只能有一种访问路径.一个访问路径就是一个执行计划. (2)执行计 ...
- 【Code Tools】AB性能测试工具(一)
一.工具下载 yum -y install httpd-tools 二.AB工具使用 格式: ab [options] [http://]hostname[:port]/path 例如:ab -n - ...
- 【Docker】iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8480 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name
启动容器的时候,出现如下错误: Error response / --dport -j DNAT --to-destination ! -i docker0: iptables: No chain/t ...
- windows 任务计划
我的需求是每天定时访问网站的某一个控制器去刷新库存 流程如下(我自己的理解) 进入任务计划页面 上图指定的bat文件内容就是访问指定的网站路径 dingshi.bat文件内容如下(这个是网上找的,可能 ...
- Mariadb/MySQL数据库单表查询基本操作及DML语句
Mariadb/MySQL数据库单表查询基本操作及DML语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一数据库及表相关概述 1>.数据库操作 创建数据库: CREATE ...
- Linux-普通用户切换为root身份
Linux 下普通用户免密码切换 root 用户 具体步骤: 1)编辑 su 文件打开第四行注释 # vim /etc/pam.d/su 2)将用户添加到 wheel 组 # usermod -G w ...