Java 请比较两个代码片段,并解释为什么一个有效,另一个无效
第一个代码段(不起作用) 配置:Java 请比较两个代码片段,并解释为什么一个有效,另一个无效,java,spring,spring-mvc,dependency-injection,Java,Spring,Spring Mvc,Dependency Injection,第一个代码段(不起作用) 配置: @Configuration public class UiConfig { @Bean //if i doesn't write this bean result will not change ApplicationWebListener getApplicationWebListener(){ return new ApplicationWebListener(); } } @Configuration p
@Configuration
public class UiConfig {
@Bean //if i doesn't write this bean result will not change
ApplicationWebListener getApplicationWebListener(){
return new ApplicationWebListener();
}
}
@Configuration
public class UiConfig {
@Bean//if I doesn't write this bean MyApplicationListener code doesn't execute
MyApplicationListener getMyApplicationListener(){
return new MyApplicationListener();
}
}
听众:
@WebListener
public class ApplicationWebListener implements ServletContextListener {
@Autowired
DatabaseHelper databaseHelper;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
databaseHelper.fillEmptyTables();
}
}
public class MyApplicationListener implements
ApplicationListener<ContextRefreshedEvent> {
@Autowired
DatabaseHelper databaseHelper;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
databaseHelper.fillEmptyTables();
}
}
结果:
java.lang.NullPointerException
at com.epam.hhsystem.web.controllers.ApplicationWebListener.contextInitialized(ApplicationWebListener.java:24)
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)
第二段:
配置:
@Configuration
public class UiConfig {
@Bean //if i doesn't write this bean result will not change
ApplicationWebListener getApplicationWebListener(){
return new ApplicationWebListener();
}
}
@Configuration
public class UiConfig {
@Bean//if I doesn't write this bean MyApplicationListener code doesn't execute
MyApplicationListener getMyApplicationListener(){
return new MyApplicationListener();
}
}
听众:
@WebListener
public class ApplicationWebListener implements ServletContextListener {
@Autowired
DatabaseHelper databaseHelper;
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("ServletContextListener started");
databaseHelper.fillEmptyTables();
}
}
public class MyApplicationListener implements
ApplicationListener<ContextRefreshedEvent> {
@Autowired
DatabaseHelper databaseHelper;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
databaseHelper.fillEmptyTables();
}
}
你能给我解释一下这些情况吗?看看你的堆栈跟踪
java.lang.NullPointerException
at com.epam.hhsystem.web.controllers.ApplicationWebListener.contextInitialized(ApplicationWebListener.java:24)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
侦听器由容器启动。@WebListener
注释告诉Servlet
容器实例化ApplicationWebListener
类这不是Spring管理的bean。因此,Spring不会自动将任何内容连接到它中,DatabaseHelper
字段保持为null
这个豆子
@Bean //if i doesn't write this bean result will not change
ApplicationWebListener getApplicationWebListener(){
return new ApplicationWebListener();
}
将保留在您的上下文中,容器根本不会使用,因为它未在容器中注册
您的第二个代码段是一个完全无关的用例。
您的第二个代码段是一个完全无关的用例。
这是什么意思?是好是坏?@gstackoverflow这取决于您的用例。Spring的ApplicationListener
的目的是对Spring事件做出反应,而不是容器事件。我理解您的意思。对容器事件做出反应的最佳方式是什么?当所有bean都被捕获时,我需要捕获事件autowiring@gstackoverflow没有最好的办法。Servlet
api有许多不同类型事件的侦听器。如果需要将Spring托管bean注册为侦听器,请使用WebApplicationInitializer
接口注册它们。