在 tomcat 开始时,自动装配:向 class 的侦听器实例发送上下文初始化事件的异常
On tomcat starts,autowired: Exception sending context initialized event to listener instance of class
我尝试使用 find all 从 db 获取所有更新,但给出了这个错误。我使用调试,我在 ServletContextListener 中看到 dbUpdatesService 为空。我认为有些东西是自动装配的,只是我想不通。在控制器 classes 中,它在 ServletContextListener 中工作正常,但不行。我尝试在 ServletContextListener class 之前放置 @Controller 或 @Component 或 @Serviece 注释,但没有帮助我。有什么想法吗?
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();
}
DbUpdateService
@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;
我的问题找到了一个很好的答案,首先是@Alan Barrows 说我不需要 new DbUpdatesService();
然后我用了这个
@Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContextUtils
.getRequiredWebApplicationContext(sce.getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
//Do something with props
...
}
我尝试使用 find all 从 db 获取所有更新,但给出了这个错误。我使用调试,我在 ServletContextListener 中看到 dbUpdatesService 为空。我认为有些东西是自动装配的,只是我想不通。在控制器 classes 中,它在 ServletContextListener 中工作正常,但不行。我尝试在 ServletContextListener class 之前放置 @Controller 或 @Component 或 @Serviece 注释,但没有帮助我。有什么想法吗?
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();
}
DbUpdateService
@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;
我的问题找到了一个很好的答案,首先是@Alan Barrows 说我不需要 new DbUpdatesService();
然后我用了这个
@Override
public void contextInitialized(ServletContextEvent sce) {
WebApplicationContextUtils
.getRequiredWebApplicationContext(sce.getServletContext())
.getAutowireCapableBeanFactory()
.autowireBean(this);
//Do something with props
...
}