V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
sandman511
V2EX  ›  程序员

[ Java ]不改变函数的入参,怎么引用前端传来的参数

  •  
  •   sandman511 · Oct 28, 2020 · 2526 views
    This topic created in 2018 days ago, the information mentioned may be changed or developed.

    前端传了 String a,String b,String c,String d 等等(或者传了一个实体类进来)
    其他类中有一个函数 hello(){xxx...}
    不改变这个函数参数的情况下,怎么在这个函数中用 a 、b 、c 、d 呢

    14 replies    2020-11-03 13:37:28 +08:00
    luman
        1
    luman  
       Oct 28, 2020
    threadlocal
    avk458
        2
    avk458  
       Oct 28, 2020
    封装一个 ctx 工具类,然后 `request.getParameter("a")`
    zlllllei
        3
    zlllllei  
       Oct 28, 2020
    threadlocal +1
    yungo8
        4
    yungo8  
       Oct 28, 2020
    前面说的都对,推荐 threadlocal 。

    如果是 spring servlet,也可以用 2 楼的。搜索关键字——service 中获取 request 对象
    gengzi
        5
    gengzi  
       Oct 28, 2020
    threadlocal
    qiyong
        6
    qiyong  
       Oct 30, 2020
    具体怎么操作?我学习一下
    qiyong
        7
    qiyong  
       Oct 30, 2020
    @sunjiayao 就放在这个应用场景下 应该怎么做?求教
    qiyong
        8
    qiyong  
       Oct 30, 2020
    @gengzi 求教
    gengzi
        9
    gengzi  
       Oct 30, 2020
    @qiyong 获取参数 d,存入 threadlocal,需要用的时候再取出来。确定不用这个参数 d 了,删除 threadlocal 的内容,防止内存泄漏。百度一下 Threadlocal 的用法就行
    qiyong
        10
    qiyong  
       Oct 31, 2020
    @gengzi 可怎么保证同一线程存取呢
    gengzi
        11
    gengzi  
       Nov 2, 2020
    @qiyong 不需要自己保证,每个请求进来,都是一个新线程。threadlocal 存储的变量仅属于当前线程,与其他线程隔离。
    qiyong
        12
    qiyong  
       Nov 2, 2020
    @gengzi 那存进去岂不是取不出来了
    sandman511
        13
    sandman511  
    OP
       Nov 2, 2020
    @gengzi 在 A.java 存 怎么在 B.java 取呢
    gengzi
        14
    gengzi  
       Nov 3, 2020
    @qiyong
    @sandman511
    百度下,看下用法就清楚了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1007 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 22:34 · PVG 06:34 · LAX 15:34 · JFK 18:34
    ♥ Do have faith in what you're doing.