• 请不要在回答技术问题时复制粘贴 AI 生成的内容
luxinfl
V2EX  ›  程序员

怎么才能安全的给 spring bean 的静态变量注入值?

  •  
  •   luxinfl · Feb 20, 2021 · 2136 views
    This topic created in 2081 days ago, the information mentioned may be changed or developed.

    用过注入非静态 setter 方法给静态变量注入,但是 jekkins 扫描代码的时候,会提示一个安全问题,然后让我们修改。这个有解麽。。。。。。 说一下场景,我需要在 yml 配置一个过期时间,然后在一个单例里面用这个值初始化。

    4 replies  •  2021-02-23 09:54:14 +08:00
    zm8m93Q1e5otOC69
    zm8m93Q1e5otOC69
    Feb 20, 2021
    1
    @Component
    public class Test {

    private static AddressService addressService;
    @Autowired
    public Test(AddressService addressService){
    Test.addressService = addressService;
    }

    public static void test(){
    addressService.addressLog();
    }
    }
    一定要用 setter 吗?我用构造函数注入的也可以用。。。
    chendy
    chendy
    Feb 20, 2021
    2
    解数据的类实现 InitializingBean,在 afterPropertiesSet 里把东西 set 给静态变量
    luxinfl
    luxinfl
    OP
    Feb 23, 2021
    3
    @beichenhpy 这个不太行,因为有另一个静态变量会用到这个参数。
    luxinfl
    luxinfl
    OP
    Feb 23, 2021
    4
    @chendy 这个是不是只能用在成员变量啊。。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3838 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 01:00 · PVG 09:00 · LAX 17:00 · JFK 20:00
    ♥ Do have faith in what you're doing.