当前位置: 首页 > news >正文

Spring整合web环境

目录

Javaweb三大组件及环境特点

Spring整合web环境的思路及实现

Spring的web开发组件spring-web

MVC框架思想及其设计思路


Javaweb三大组件及环境特点

 

Spring整合web环境的思路及实现

 

package com.xfy.listener;import com.xfy.config.SpringConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;public class ContextLoaderListenter implements ServletContextListener {private  String CONTEXT_CONFIG_LOCATION="ContextConfigLocation";@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {ServletContext servletContext = servletContextEvent.getServletContext();String initParameter = servletContext.getInitParameter(CONTEXT_CONFIG_LOCATION);
//        initParameter=initParameter.substring("classpath".length());//1.创建Spring容器ApplicationContext applicationContext=new ClassPathXmlApplicationContext(initParameter);//2.将容器存入ServletContext中servletContext.setAttribute("applicationContext",applicationContext);}@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {}
}public class WebAppLicationContext {public  static ApplicationContext getWebAppLicationContext(ServletContext servletContext){ApplicationContext applicationContext = (ApplicationContext) servletContext.getAttribute("applicationContext");return applicationContext;}
}@WebServlet(urlPatterns = "/accountServlet")
public class AccountServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ApplicationContext applicationContext =  WebAppLicationContext.getWebAppLicationContext(request.getServletContext());AcountService bean = applicationContext.getBean(AcountService.class);bean.transforMoney("tom","lucy",500);}

 

Spring的web开发组件spring-web

 Spring其实已经为我们定义好了一个ContextLoaderListener,使用方式跟我们上面自己定义的大体一样

 先导入Spring-web的坐标

    <dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.2.5.RELEASE</version></dependency><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<!--    定义全局参数  --><context-param><param-name>contextClass</param-name><param-value>com.xfy.config.MyxfyApplicationContext</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
</web-app>

 

MVC框架思想及其设计思路

 

 

http://www.lryc.cn/news/249534.html

相关文章:

  • 分享从零开始学习网络设备配置--任务4.3 使用动态路由RIPng实现网络连通
  • vue2.0+elementui集成file-loader之后图标失效问题
  • C# 文件帮助类(FileHelper)
  • WordPress 外链跳转插件
  • 算法的10大排序
  • “十道机器学习问题,帮助你了解基础知识和常见算法“
  • 部署WAF安全应用防火墙(openresty部署)
  • yml转properties工具
  • zerotier 搭建 moon中转服务器 及 自建planet
  • 深入了解Rabbit加密技术:原理、实现与应用
  • Linux常用命令——mv命令
  • Panalog 日志审计系统 前台RCE漏洞复现
  • Android设置文字颜色渐变
  • java基础面试题(二)
  • php爬虫实现把目标页面变成自己的网站页面
  • [c语言c++]手写你自己的swap交换函数
  • 技术类知识汇总(二)
  • 简单好用!日常写给 ChatGPT 的几个提示词技巧
  • pytorch分布式训练
  • 【PyTorch】(三)模型的创建、参数初始化、保存和加载
  • 高效开发之:判断复杂list中的对象属性是否包含某个值
  • MacOS + Android Studio 通过 USB 数据线真机调试
  • 部署jekins遇到的问题
  • SQLY优化
  • 设计模式——行为型模式(一)
  • Rust语言入门教程(六) - 字符串类型
  • 【MATLAB源码-第92期】基于simulink的QPSK调制解调仿真,采用相干解调对比原始信号和解调信号。
  • 关于C语言控制浮点数输出精度问题
  • 【Linux 静态IP配置】
  • 【Linux 操作系统配置 SFTP】