我正在使用@PostCostruct批注获取初始化方法,以在启动Sprin Boot应用程序时运行代码。

@Service("jobManager")
public class JobManager {

    @Autowired
    SchedulerFactoryBean scheduler;

    @Autowired
    InterruttoreService interruttoreService;

    @PostConstruct
    public void createInitialJobs() throws SchedulerException {
        List<Interruttore> interruttori = interruttoreService.findAllSwitches();

        for (int i = 0; i < interruttori.size(); i++) {
            Interruttore interruttore = interruttori.get(i);
            interruttoreService.toggleSwitchOnStartup(interruttore);

        }

    }


interruttoreService.toggleSwitchOnStartup(interruttore)
在服务类中,并且是以下类(作业管理器在该类中自动连接)

@Override
    public void toggleSwitchOnStartup(Interruttore interruttore) {
        Date nextTimeout = interruttore.getTimeoutDate();
        Date date = new Date();
        Date now = new Timestamp(date.getTime());
        if (nextTimeout == null) {
            nextTimeout = now;
        }
        int idInterruttore = interruttore.getIdInterruttore();

        if(nextTimeout.after(now)){ //c'è un timeout futuro

            try { lightOn(idInterruttore);
            jobManager.createJob(interruttore, nextTimeout);
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        } else {
            shutDown(idInterruttore);
        }

    }


从此方法调用jobManager时出现NullPointerException,不知道为什么在@PostCostruct期间创建了bean。

这是错误的堆栈跟踪

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'interruttoreService': Unsatisfied dependency expressed through field 'jobManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobManager': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
    ... 53 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobManager': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1575)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
    ... 66 more
Caused by: java.lang.NullPointerException
    at it.besmart.service.InterruttoreServiceImpl.toggleSwitchOnStartup(InterruttoreServiceImpl.java:112)
    at it.besmart.service.InterruttoreServiceImpl$$FastClassBySpringCGLIB$$907e162e.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at it.besmart.service.InterruttoreServiceImpl$$EnhancerBySpringCGLIB$$e2a6ab4b.toggleSwitchOnStartup(<generated>)
    at it.besmart.quartz.JobManager.createInitialJobs(JobManager.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    ... 78 more


interruttoreService已自动正确连接,因为启动后,它立即照常工作。

Rergarding jobManager,我只是以这种方式在InterruttoreService.class中@Autowire

@Service("interruttoreService")
@Transactional
public class InterruttoreServiceImpl implements InterruttoreService {

    @Autowired
    JobManager jobManager;
....


....

最佳答案

这是紧密耦合的代码(将InterruttoreService注入到JobManager中,同时将JobManager注入到InterruttoreService中)。

为什么不从InterruttoreService中删除​​JobManager注入,并用toggleSwitchOnStartup()带注释的方法在所有@PostCostruct中直接完成InterruttoreService,而JobManager已直接连接。

09-11 12:37