Inject the static properties of the class into the instance in the spring container

Generalize

@Autowired is an instance that can only be injected into the spring container for non-static properties of the class, and the instance that wants to inject static properties into the spring container can be passed in the non-static method marked by @Autowired.

Code demo

@Component
public class SubversionApiWrapper implements CodeApiInterface {
    
    

    //spring实例本类的bean时,通过setter方法初始化静态成员变量
    private static ProjectBiz projectBiz;
           
    //setProjectBiz()方法也不能使用static来修饰,否则不会执行
    @Autowired 
    public void setProjectBiz(ProjectBiz projectBiz) {
    
    
        SubversionApiWrapper.projectBiz = projectBiz;
    }
	
  // 其余代码,省略一万行
}

Guess you like

Origin blog.csdn.net/nangonghen/article/details/102057374