spring非controller层使用@Autowired注解注入的bean

一,使用spring的注解从而使用依赖注入注入。

   @Component
   public class SynchroDataQuartz

二,使用@Autowired注解注入需要用到的bean。

    @Autowired
    private InstitutionService institutionService;
    
    @Autowired
    private UserService userService;

三,使用本类中加载静态的方式获取自己。

    public static SynchroDataQuartz synchroDataQuartz;

四,使用@PostConstruct注解和重写init方法使其初始化时注入。

    @PostConstruct
    public void init(){
        synchroDataQuartz = this;
        synchroDataQuartz.userService = this.userService;
        synchroDataQuartz.institutionService = this.institutionService;
    }

注:该类一定要背spring主动注入扫描到

猜你喜欢

转载自blog.csdn.net/weixin_41929877/article/details/81450860