如何在Java Web应用程序中声明启动挂钩
我想在web应用程序启动时进行一些初始化(计时器和日志记录)。我不能使用ContextListener,因为我没有在其中获得任何Spring组件。我需要一个简单的钩子,比如“afterWebAppHasLoadedAndEverythingyIsSetUp()”。如何在Java Web应用程序中声明启动挂钩,java,spring,servlets,Java,Spring,Servlets,我想在web应用程序启动时进行一些初始化(计时器和日志记录)。我不能使用ContextListener,因为我没有在其中获得任何Spring组件。我需要一个简单的钩子,比如“afterWebAppHasLoadedAndEverythingyIsSetUp()”。 它存在吗?加载bean时,ApplicationContext会发布某些类型的事件 ApplicationContext中的事件处理通过ApplicationEvent类和ApplicationListener接口提供。因此,如果一个
它存在吗?加载bean时,ApplicationContext会发布某些类型的事件 ApplicationContext中的事件处理通过ApplicationEvent类和ApplicationListener接口提供。因此,如果一个bean实现了ApplicationListener,那么每当ApplicationEvent发布到ApplicationContext时,就会通知该bean spring提供了以下事件
- 上下文刷新事件
- ContextStartedEvent
- 上下文停止事件
- 上下文
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
import org.springframework.context.ApplicationListener;
导入org.springframework.context.event.ContextStartedEvent;
公共类CStartEventHandler
实现ApplicationListener{
ApplicationEvent(ContextStartedEvent事件)上的公共无效{
System.out.println(“接收到ContextStartedEvent”);
}
}
然后,您只需在Spring中将该类注册为bean。加载bean时,ApplicationContext会发布某些类型的事件 ApplicationContext中的事件处理通过ApplicationEvent类和ApplicationListener接口提供。因此,如果一个bean实现了ApplicationListener,那么每当ApplicationEvent发布到ApplicationContext时,就会通知该bean spring提供了以下事件
- 上下文刷新事件
- ContextStartedEvent
- 上下文停止事件
- 上下文
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class CStartEventHandler
implements ApplicationListener<ContextStartedEvent>{
public void onApplicationEvent(ContextStartedEvent event) {
System.out.println("ContextStartedEvent Received");
}
}
import org.springframework.context.ApplicationListener;
导入org.springframework.context.event.ContextStartedEvent;
公共类CStartEventHandler
实现ApplicationListener{
ApplicationEvent(ContextStartedEvent事件)上的公共无效{
System.out.println(“接收到ContextStartedEvent”);
}
}
然后您只需将该类注册为Spring的bean。如果您使用的是Spring Boot(我推荐),那么您可以在应用程序启动后使用以下CommandLineRunner bean来运行初始化内容。您将能够准备好所有Springbean,下面是代码片段
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MyApplication extends SpringBootServletInitializer {
// here you can take any number of object which is anutowired automatically
@Bean
public CommandLineRunner init(MyService service, MyRepository repository) {
// do your stuff with the beans
return null;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
如果您使用的是SpringBoot(我推荐),那么可以在应用程序启动后使用以下CommandLineRunner bean来运行初始化。您将能够准备好所有Springbean,下面是代码片段
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MyApplication extends SpringBootServletInitializer {
// here you can take any number of object which is anutowired automatically
@Bean
public CommandLineRunner init(MyService service, MyRepository repository) {
// do your stuff with the beans
return null;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}