我有这堂课:
public class DatabaseHelper {
@Autowired
static UtilService utilService;
public static void fillEmptyTables(){
if(!isSkillsInDatabase()){
utilService.addGeneratedSkill();
}
if(!isEventTypesInDatabase()){
utilService.addGeneratedEventType();
}
if(!isEventStatusesInDatabase()){
utilService.addGeneratedEventStatus();
}
}
public static boolean isSkillsInDatabase(){
return utilService.getAllSkills().size() != 0;
}
public static boolean isEventStatusesInDatabase(){
return utilService.getAllEventStatuses().size() != 0;
}
public static boolean isEventTypesInDatabase(){
return utilService.getAllEventTypes().size() != 0;
}
}
和这个听众:
@WebListener
public class ApplicationWebListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
DatabaseHelper.fillEmptyTables();
}
}
当我运行我的应用程序时,我看到以下内容:
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 10029 ms
ServletContextListener started
22.10.2013 13:21:16 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class com.epam.hhsystem.web.controllers.ApplicationWebListener
java.lang.NullPointerException
at com.epam.hhsystem.util.DatabaseHelper.isSkillsInDatabase(DatabaseHelper.java:25)
at com.epam.hhsystem.util.DatabaseHelper.fillEmptyTables(DatabaseHelper.java:13)
at com.epam.hhsystem.web.controllers.ApplicationWebListener.contextInitialized(ApplicationWebListener.java:19)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
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)
22.10.2013 13:21:16 org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
这个问题的原因是什么?
如何解决?
更新
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.epam.hhsystem.web.controllers"/>
</beans:beans>
和这个:
<import resource="classpath:spring/BeanConfig.xml" />
<!-- Файл с настройками Security -->
<import resource="security_config.xml" />
和这个:
<context:annotation-config />
<!--ищем тут разные аннотации (например @service) -->
<context:component-scan base-package="com.epam.hhsystem.jpa" />
<context:component-scan base-package="com.epam.hhsystem.util" />
<context:component-scan base-package="com.epam.hhsystem.services" />
<context:component-scan base-package="com.epam.hhsystem.web.controllers" />
<!-- Файл с настройками ресурсов для работы с данными (Data Access Resources) -->
<import resource="data.xml" />
这么。
更新
@WebListener
public class ApplicationWebListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
}
}
这段代码效果很好。当我运行我的应用程序时,我看到
servletContextListener 已启动
信息
实用服务位置:
来自配置的字符串:
<context:component-scan base-package="com.epam.hhsystem.services" />
将此代码添加到@Configuration 类之后
@Bean
ApplicationWebListener getApplicationWebListener(){
return new ApplicationWebListener();
}
我看到了旧错误。
更新 2
为了您的建议,我重构了我的代码。我很困惑:
变体1:
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
UtilService utilService;
public void fillEmptyTables(){
if(!isSkillsInDatabase()){
utilService.addGeneratedSkill();
}
if(!isEventTypesInDatabase()){
utilService.addGeneratedEventType();
}
if(!isEventStatusesInDatabase()){
utilService.addGeneratedEventStatus();
}
}
public boolean isSkillsInDatabase(){
return utilService.getAllSkills().size() != 0;
}
public boolean isEventStatusesInDatabase(){
return utilService.getAllEventStatuses().size() != 0;
}
public boolean isEventTypesInDatabase(){
return utilService.getAllEventTypes().size() != 0;
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
fillEmptyTables();
}
}
这种变体效果很好,但它是架构恐怖
变体 2:
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
DatabaseHelper.fillEmptyTables();
}
}
我看到旧的堆栈跟踪。
为什么?