@Autowired in ServletContextListener

前端 未结 4 1621
一生所求
一生所求 2021-02-06 06:04

i hava aclass InitApp

@Component
public class InitApp implements ServletContextListener {

@Autowired
ConfigrationService weatherConfService;

/** Creates a new          


        
4条回答
  •  猫巷女王i
    2021-02-06 07:02

    @WebListener
    public class StartupListener implements ServletContextListener {
    
        @Autowired
        private MyRepository repository;
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
        }
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            AutowireCapableBeanFactory autowireCapableBeanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext()).getAutowireCapableBeanFactory();
            autowireCapableBeanFactory.autowireBean(this);
    
            repository.doSomething();
        }
    }
    

提交回复
热议问题