如何在非controller层,注入service层
下面我们将UserInfoService注入到DataUpLoad 类中,步骤如下:
- 首先要在需要用到的类加@Component
- 使用@PostConstruct将注入的对象交给静态对象管理
@Component public class DataUpLoad { @Autowired UserInfoService userInfoServiceAuto; //注入的对象 private static UserInfoService userInfoService; //静态对象 @PostConstruct public void init(){ userInfoService = this.userInfoServiceAuto; //将注入的对象交给静态对象管理 } }
3然后就能通过 userInfoService。出需要的