为类的静态属性注入spring容器中的实例

概括

@Autowired是只能为类的非静态属性注入spring容器中的实例,想为静态属性注入spring容器中的实例可通过在@Autowired标记的非静态方法中。

代码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;
    }
	
  // 其余代码,省略一万行
}

猜你喜欢

转载自blog.csdn.net/nangonghen/article/details/102057374