Код: Выделить всё
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
Как это исправить?
ОБНОВЛЕНИЕ
Код: Выделить всё
Код: Выделить всё
Код: Выделить всё
Код: Выделить всё
@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 запущен
Местоположение UtilService:

строку из конфигурации:
Код: Выделить всё
Код: Выделить всё
@Bean
ApplicationWebListener getApplicationWebListener(){
return new ApplicationWebListener();
}
ОБНОВЛЕНИЕ 2
Я провел рефакторинг своего кода по вашему совету. Я в замешательстве:
Вариант 1:
Код: Выделить всё
public class MyApplicationListener implements ApplicationListener {
@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 {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
DatabaseHelper.fillEmptyTables();
}
}
Почему?
Подробнее здесь: https://stackoverflow.com/questions/195 ... e-of-class
Мобильная версия