spring-boot 不能直接对静态变量进行自动注入

因为注入时相当于调用的是get和set方法,静态变量没有这个所以不行,如果非要给静态变量注入的话,可以先构造一个静态变量,然后使用非静态类帮他注入。但是接口的话好像不行(我没有成功)

    //对静态变量注入
    private static DownRepository downRepository;
    @Autowired
    public void setDownRepository(DownRepository downRepository) {
    
    
        TcpUtils.downRepository = downRepository;
    }

被注解的类新建对象时也需注解,例如先在ControllerUtils类里注入了两个变量,new这个对象时需在头上加上注解。否则这两个值就为null,因为相当于没有赋值。

    @Autowired
    ControllerUtils controllerUtils=new ControllerUtils();

猜你喜欢

转载自blog.csdn.net/wflsyf/article/details/118016925