我使用@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,不知道为什么bean是在@PostCostruct期间创建的......
这是错误的堆栈跟踪
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;
....
....
答案 0 :(得分:3)
这是紧密耦合的代码(您InterruttoreService
已注入JobManager
,同时JobManager
注入InterruttoreService
)。
为什么不从InterruttoreService
删除JobManager
注入,让toggleSwitchOnStartup()
在@PostCostruct
带注释的方法中完成InterruttoreService
直接JobManager
{ {1}}已经连线。
答案 1 :(得分:0)
问题是您的服务名称
的小错字您有服务
@Service("jobManager")//first j lowercase
public class JobManager {
@Autowired //In your Autowired if you dont say a qualifier name will try to find names as "JobManager" first J uppercase
JobManager jobManager;
解决方案:
删除服务中的名称仅保留
@Service
public class JobManager {
添加限定符
@Autowired
@Qualifier("jobManager")
JobManager jobManager;