zoukankan      html  css  js  c++  java
  • SpringBoot定时任务 Service层无法注入问题

    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    @Component
    public class ApplicationContextUtil implements ApplicationContextAware {
    
        private static ApplicationContext applicationContext;
    
        public static ApplicationContext getApplicationContext() {
            return applicationContext;
        }
    
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            ApplicationContextUtil.applicationContext = applicationContext;
    
        }
    
    
        public static Object getBean(String beanName) {
            return applicationContext.getBean(beanName);
        }
    }

    调用:

    AppLogServiceImpl appLogService = (AppLogServiceImpl) ApplicationContextUtil.getBean("appLogServiceImpl");

    Ps: getBean中名称是Service中的名称,如果Service中没有自定义名称,那就把实现类的首字母小写即可。

    转自:https://www.cnblogs.com/doudou2018/p/10416043.html

  • 相关阅读:
    5.14事务
    5.13Mysql数据库Database
    未来打算
    浅谈P NP NPC
    1222
    1219
    Linux初等命令
    惩罚因子(penalty term)与损失函数(loss function)
    12 14
    java 泛型思考
  • 原文地址:https://www.cnblogs.com/julian-chang/p/11897021.html
Copyright © 2011-2022 走看看