gpt4 book ai didi

java - 在 tomcat 启动时, Autowiring : Exception sending context initialized event to listener instance of class

转载 作者:行者123 更新时间:2023-11-30 11:08:44 26 4
gpt4 key购买 nike

我尝试使用 find all 从 db 获取所有更新,at 给出了这个错误。我使用调试,我在 ServletContextListener 中看到 dbUpdatesService 为空。我认为有些东西是 Autowiring 的,只是我想不通。在 Controller 类中,它在 ServletContextListener 中工作正常,但不行。我尝试将 @Controller 或 @Component 或 @Serviece 注释放在 ServletContextListener 类之前,但没有帮助我。有什么想法吗?

ServletContextListener

public class HRMSServletContextListener implements ServletContextListener {
@Autowired
private IDbUpdatesService dbUpdatesService = new DbUpdatesService();

@Override
public void contextInitialized(ServletContextEvent arg0) {
LOGGER.info("Servlet context created.");

chechFolderExistence();

checkNewUpdate();
}

private void checkNewUpdate(){
LOGGER.info("Cheching for new Updates.");
List<DbUpdates> updates = new ArrayList<DbUpdates>();

updates = dbUpdatesService.findAll();
}

spring-config.xml

        <context:annotation-config/>
<context:component-scan base-package="com.atni.hrms" />
<context:property-placeholder location="classpath:database.properties" />

<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
name="txManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

抽象服务

@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)

最佳答案

不确定是否应该在需要 Autowiring 时创建新对象

@Autowired
private IDbUpdatesService dbUpdatesService = new DbUpdatesService();

尝试

@Autowired
private IDbUpdatesService dbUpdatesService;

关于java - 在 tomcat 启动时, Autowiring : Exception sending context initialized event to listener instance of class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28434915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com