码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • javaWeb使用spring框架时在配置上的编程技巧


     在上一篇文章Spring之IOC_数字公民某杨的博客-CSDN博客

    中使用spring后,示例代码中有两行

    ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    studentLoginService = (StudentLoginService) ac.getBean("studentLoginService");

    这两行代码的意思是,获取spring容器 ac ,然后从ac对象中获取studentLoginService对象。

    在这里有些技巧,

    一个是ac这个spring容器对象创建好以后,实际只需要从里面获取studentLoginService对象,希望用垃圾回收机制尽快回收ac。

    而当方法结束时,方法出栈,对对象的引用ac变量销毁,这样就启动java垃圾回收机制,回收原来ac指向的对象。如果还是放在servlet的service方法中,servlet对象只创建一次,但是service方法应该是多线程调用,所以会造成多次创建ac对象。而我们知道servlet有个init方法,是在servlet对象创建时调用,这样就想到把ac创建代码放在init方法中,我们有:

    public class StudentServlet extends HttpServlet {
        private StudentLoginService studentLoginService;
    
        @Override
        public void init() throws ServletException {
            ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
            studentLoginService = (StudentLoginService) ac.getBean("studentLoginService");
        }
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String name = req.getParameter("name");
            String address = req.getParameter("address");
            try {
                Student student = studentLoginService.studentLogin(name, address);
    
    
                HttpSession session = req.getSession();
                ServletContext servletContext = this.getServletContext();
                HttpSession tempSession = (HttpSession) servletContext.getAttribute(student.getId()+"");
                if(tempSession!=null){
                    tempSession.invalidate();
                }else {
                    servletContext.setAttribute(student.getId()+"", session);
                }
                session.setAttribute("student", student);
    
                resp.sendRedirect("sucess.jsp");
            }catch (Exception e){
                resp.sendRedirect("error.jsp");
            }
        }

    另一个技巧体现在web.xml配置文件中,先看下图:

     

    我们有web.xml:


        contextConfigLocation
        classpath:spring-config.xml


    这个配置是说,在servletContext对象中保存以contextConfigLocation为key,以classpath:spring-config.xml为值的数据。这样当spring=config.xml后面需要修改的时候,就不需要去代码里面修改。


        org.springframework.web.context.ContextLoaderListener

    这个配置的意思是监听servletContext对象的创建,然后在监听器中,实现spring配置文件内容的读取。由于servlet对象只有1个,这样就减少了servlet创建多次时,对spring-config.xml文件的多次io操作

  • 相关阅读:
    代码随想录训练营第37天|LeetCode 738.单调递增的数字、714. 买卖股票的最佳时机含手续费、 968.监控二叉树
    redis bitmap数据结构之java对等操作
    Java系列 | 如何讲自己的JAR包上传至阿里云maven私有仓库【云效制品仓库】
    rpc网络
    Android用户登录与数据存储:从权限请求到内外部存储的完整实践【完整实践步骤、外部存储、内部存储】
    Apache DolphinScheduler如何完全设置东八区?
    【AD-NeRF】音频驱动人脸NeRF
    艺术与科技的狂欢,阿那亚2022砂之盒沉浸艺术季
    使用日志进行调查 - SQL 注入攻击示例
    Spring Cloud Alibaba微服务第22章之Oauth2
  • 原文地址:https://blog.csdn.net/m0_47161778/article/details/126697389
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号