如何在非controller层,注入service层

如何在非controller层,注入service层

下面我们将UserInfoService注入到DataUpLoad 类中,步骤如下:

  1. 首先要在需要用到的类加@Component
  2. 使用@PostConstruct将注入的对象交给静态对象管理
    @Component
     public class DataUpLoad { 
    
    	@Autowired
    	UserInfoService userInfoServiceAuto; //注入的对象
    
    	private static UserInfoService userInfoService;  //静态对象
    
    	@PostConstruct
    	public void init(){
    		userInfoService = this.userInfoServiceAuto;  //将注入的对象交给静态对象管理
    	}
     
    }
    

    3然后就能通过 userInfoService。出需要的

猜你喜欢

转载自blog.csdn.net/zhw0596/article/details/104914477