Spring整合web环境

news/2025/3/14 22:52:35/

目录

1.添加pom

2.配置web.xml   spring.xml

4.配置在创建一个类实现ServletContextListener

5.配置servlet


1.添加pom

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.3.29</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>4.0.1</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>5.3.29</version>

</dependency>

2.配置web.xml   spring.xml

web.xml

<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

spring

<context:component-scanbase-package="com.hu"/>

4.配置在创建一个类实现ServletContextListener

创建一个类实现ServletContextListener

将创建好的ApplicationContext存储到ServletContext域中,这样整个web层任何位置就都可以获取到了

服务器一启动就执行Initial方法这个方法是整个web最先执行的

*sce就是全局对象servletContext

public class MyListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {ServletContext servletContext = sce.getServletContext();String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");contextConfigLocation = contextConfigLocation.substring("classpath".length());ApplicationContext applicationContext = new ClassPathXmlApplicationContext(contextConfigLocation);servletContext.setAttribute("applicationContext",applicationContext);}

5.配置servlet

@WebServlet("/login")
publicclassLoginControllerextendsHttpServlet{
@Override
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//ServletContextservletContext=this.getServletContext();
ServletContextservletContext=req.getServletContext();
WebApplicationContextwebApplicationContext=WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserServicebean=webApplicationContext.getBean("us",UserService.class);
bean.show();
}


http://www.ppmy.cn/news/1018643.html

相关文章

界面控件DevExpress WPF Chart组件——拥有超快的数据可视化库!

DevExpress WPF Chart组件拥有超大的可视化数据集&#xff0c;并提供交互式仪表板与高性能WPF图表库。DevExpress Charts提供了全面的2D / 3D图形集合&#xff0c;包括数十个UI定制和数据分析/数据挖掘选项。 PS&#xff1a;DevExpress WPF拥有120个控件和库&#xff0c;将帮助…

一种水文水利行业满管非满管双声道流量计安装调试

供电电源 用户应该特别注意&#xff1a;若是交流&#xff08;AC220V&#xff09;供电的主机插入直流电源&#xff0c;或者直流&#xff08;DC24V&#xff09;供电的主机接入AC220V电源&#xff0c;就会把流量计烧毁。 普通主机&#xff08;包括固定式主机、盘装式主机&#x…

【剑指 Offer 40】最小的k个数

题目&#xff1a; 输入整数数组 arr &#xff0c;找出其中最小的 k 个数。例如&#xff0c;输入 4、5、1、6、2、7、3、8 这 8 个数字&#xff0c;则最小的 4 个数字是 1、2、3、4。 示例&#xff1a; 输入&#xff1a;arr [3,2,1], k 2 输出&#xff1a;[1,2] 或者 [2,1] …

【Django】招聘面试管理01 创建项目运行项目

文章目录 前言一、创建项目二、运行项目三、访问后台管理页面四、配置项总结 前言 跟着视频学一学&#xff0c;记录一下。 一、创建项目 照着步骤创建虚拟环境&#xff0c;安装Django等依赖包&#xff0c;创建项目&#xff1a;【Django学习】01 项目创建、结构及命令 > d…

String 类的运用

目录 1.字符串构造 2.String对象的比较 2.1比较是否引用同一个对象 2. 2boolean equals(Object anObject) 2.3int compareTo(String s) 方法: 按照字典序进行比较 2.4int compareToIgnoreCase(String str) 3.字符串查找 4.2大小写转换 4.3字符串转数组 4.4 格式化 5.字…

TCP滑动窗口和拥塞控制

目录 滑动窗口什么是滑动窗口为什么要使用滑动窗口滑动窗口的工作原理滑动窗口会出现的几种问题数据包丢失怎么解决&#xff1f;ACK丢失怎么解决&#xff1f; 拥塞控制拥塞控制是什么&#xff1f;拥塞控制的实现理解拓展&#xff1a;拥塞控制是如何判断网络拥塞情况的&#xff…

Linux在终端显示Git分支等信息

环境&#xff1a;RHEL7.5 在环境变量文件中添加以下内容&#xff1a; function git-branch-name { git symbolic-ref --short -q HEAD 2>/dev/null } function git-branch-prompt {local branchgit-branch-nameif [ $branch ]; then printf "(%s)" $branch; fi }…

【软件工程】3 ATM系统的设计

目录 3 ATM系统的设计 3.1体系结构设计 3.2 设计模式选择 3.3 补充、完善类图 3.4 数据库设计 3.4.1 类与表的映射关系 3.4.2 数据库设计规范 3.4.3 数据库表 3.5 界面设计 3.5.1 界面结构设计 3.5.2 界面设计 3.5.2.1 功能界面设计 3.5.2.2 交互界面 总博客&…