我有这堂课:
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已启动
消息
UtilService位置:
来自配置的字符串:
<context:component-scan base-package="com.epam.hhsystem.services" />
将此代码添加到@Configuration类
之后 @Bean
ApplicationWebListener getApplicationWebListener(){
return new ApplicationWebListener();
}
我看到了错误。
更新2
我重构了我的代码以征求您的意见。我很困惑:
VARIANT1:
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();
}
}
我看到旧的堆栈跟踪。
为什么?
答案 0 :(得分:0)
确保在Spring配置文件中启用了组件扫描。主要扫描标记应指向包含UtilService
的包。我想知道UtilService
目前没有被初始化为Spring bean,因此它不是自动装配的候选者。
<context:component-scan base-package="package.containing.UtilService" />
尝试在Spring配置中将ApplicationWebListener
注册为bean,我不确定这是否可行,但为了在类中自动装配字段,该类必须是bean。 Spring无法将bean自动装配到未作为IOC容器的一部分实例化的类中。
还有另一种方法。 Spring提供了一个ApplicationListener
接口,可以由类实现,然后注册为bean。 ApplicationListener
将在应用程序生命周期的各个时间点调用,例如启动,重启和关闭。
以下是ApplicationListener
:
在配置文件中注册该侦听器的示例,该文件必须是调度程序的配置文件: