import org. springframework. context. ApplicationEvent ;
public class MyEvent extends ApplicationEvent {
public MyEvent ( Object source) {
super ( source) ;
}
}
import com. xfr. event. MyEvent ;
import org. springframework. context. ApplicationListener ;
import org. springframework. stereotype. Component ;
@Component
public class MyApplicationListener implements ApplicationListener < MyEvent > {
@Override
public void onApplicationEvent ( MyEvent event) {
System . out. println ( "资源" + event. getSource ( ) ) ;
System . out. println ( "事件:" + event. toString ( ) ) ;
}
}
封装SpringContextUtils,以便于使用上下文对象:ApplicationContext,封装publishEvent方法
import org. springframework. beans. BeansException ;
import org. springframework. beans. factory. NoSuchBeanDefinitionException ;
import org. springframework. context. ApplicationContext ;
import org. springframework. context. ApplicationContextAware ;
import org. springframework. context. ApplicationEvent ;
import org. springframework. core. env. Environment ;
import org. springframework. core. io. Resource ;
import org. springframework. stereotype. Component ;
import java. io. IOException ;
import java. lang. annotation. Annotation ;
import java. util. Optional ;
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static String getBeanName ( Class clazz) {
String [ ] beanNamesForType = applicationContext. getBeanNamesForType ( clazz) ;
if ( beanNamesForType. length > 0 ) {
return beanNamesForType[ 0 ] ;
}
return null ;
}
@Override
public void setApplicationContext ( ApplicationContext applicationContext) throws BeansException {
Optional . ofNullable ( applicationContext) . orElse ( SpringContextUtils . applicationContext = applicationContext) ;
}
public static ApplicationContext getApplicationContext ( ) {
return applicationContext;
}
public static void publishEvent ( ApplicationEvent event) {
applicationContext. publishEvent ( event) ;
}
public static Object getBean ( String name) throws BeansException {
return applicationContext. getBean ( name) ;
}
public static < T > T getBean ( Class < T > aClass) {
return applicationContext. getBean ( aClass) ;
}
public static < T > T getBean ( String name, Class < T > requiredType) throws BeansException {
return applicationContext. getBean ( name, requiredType) ;
}
public static boolean containsBean ( String name) {
return applicationContext. containsBean ( name) ;
}
public static boolean isSingleton ( String name) throws NoSuchBeanDefinitionException {
return applicationContext. isSingleton ( name) ;
}
public static Class getType ( String name) throws NoSuchBeanDefinitionException {
return applicationContext. getType ( name) ;
}
public static String [ ] getAliases ( String name) throws NoSuchBeanDefinitionException {
return applicationContext. getAliases ( name) ;
}
public static Environment getEnvironment ( ) {
return applicationContext. getEnvironment ( ) ;
}
public static < A extends Annotation > A findAnnotationOnBean ( String beanName, Class < A > annotationType)
throws NoSuchBeanDefinitionException {
return applicationContext. findAnnotationOnBean ( beanName, annotationType) ;
}
public static boolean containsLocalBean ( String name) {
return applicationContext. containsLocalBean ( name) ;
}
public static Resource [ ] getResources ( String locationPattern) throws IOException {
return applicationContext. getResources ( locationPattern) ;
}
import com. xfr. event. MyEvent ;
import com. xfr. extension. SpringContextUtils ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
public class EventController {
@GetMapping ( "event" )
public String event ( String eventName) {
SpringContextUtils . publishEvent ( new MyEvent ( eventName) ) ;
return "success" ;
}
}