Java 在tomcat启动时,自动连线:异常将上下文初始化事件发送到类的侦听器实例
我尝试使用find all从db获取所有更新,但出现此错误。我使用debug,并在ServletContextListener中看到DbUpdateService为null。我觉得autowired有些问题,只是我不明白。在控制器类中,它只在ServletContextListener中正常工作,而不是在ServletContextListener中。我尝试将@Controller或@Component或@service注释放在ServletContextListener类之前,但没有帮助我。有什么想法吗 ServletContextListenerJava 在tomcat启动时,自动连线:异常将上下文初始化事件发送到类的侦听器实例,java,spring,tomcat,autowired,Java,Spring,Tomcat,Autowired,我尝试使用find all从db获取所有更新,但出现此错误。我使用debug,并在ServletContextListener中看到DbUpdateService为null。我觉得autowired有些问题,只是我不明白。在控制器类中,它只在ServletContextListener中正常工作,而不是在ServletContextListener中。我尝试将@Controller或@Component或@service注释放在ServletContextListener类之前,但没有帮助我。有
不确定要自动连接时是否应创建新对象
@Autowired
private IDbUpdatesService dbUpdatesService = new DbUpdatesService();
试一试
我在我的问题上找到了一个很好的答案,首先是@Alan Barrows如何说我不需要
new DbUpdateService()代码>我用了这个
@Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContextUtils
.getRequiredWebApplicationContext(sce.getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
//Do something with props
...
}
@Override
@Transactional("txManager")
public List<T> findAll() {
return getDao().findAll();
}
@Service("iDbUpdatesService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class DbUpdatesService extends AbstractService<DbUpdates, Long> implements IDbUpdatesService{
@Autowired
private IDbUpdatesDao DbUpdatesDao;
public DbUpdatesService() {
super();
}
public void setDbUpdatesDao(IDbUpdatesDao DbUpdatesDao) {
this.DbUpdatesDao = DbUpdatesDao;
}
@Override
protected IOperations<DbUpdates, Long> getDao() {
return DbUpdatesDao;
}
}
SEVERE: Exception sending context initialized event to listener instance of class com.atni.hrms.persistence.model.listeners.HRMSServletContextListener
java.lang.NullPointerException
at com.awinta.hrms.persistence.service.common.AbstractService.findAll(AbstractService.java:24)
at com.awinta.hrms.persistence.model.listeners.HRMSServletContextListener.checkNewUpdate(HRMSServletContextListener.java:69)
at com.awinta.hrms.persistence.model.listeners.HRMSServletContextListener.contextInitialized(HRMSServletContextListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
@Autowired
private IDbUpdatesService dbUpdatesService = new DbUpdatesService();
@Autowired
private IDbUpdatesService dbUpdatesService;
@Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContextUtils
.getRequiredWebApplicationContext(sce.getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
//Do something with props
...
}