- 实现ApplicationListener 接口
- 通过@EvenListener 方法
- @EventListener 原理
事件
static class MyEvent extends ApplicationEvent {
public MyEvent(Object source) {
super(source);
}
}
主线
@Component
static class MyService{
@Autowired
private ApplicationEventPublisher publisher; // applicationContext
public void doBusiness() {
log.debug("主线业务");
// 主线业务完成后需要做一些支线业务,下面是问题代码
publisher.publishEvent(new MyEvent("MyService.doBusiness()"));
}
}
------------------------------------------
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A48_1.class);
context.getBean(MyService.class).doBusiness();
context.close();
}
解耦后操作
@Component
static class SmsApplicationListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
log.debug("发送短信");
}
}
----------------------------------------------------------------------------
@Component
static class EmailApplicationListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
log.debug("发送邮件");
}
}
只需要修改解耦部分
@Component
static class SmsApplicationListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
log.debug("发送短信");
}
}
----------------------------------------------------------------------------
@Component
static class EmailApplicationListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
log.debug("发送邮件");
}
}
@Component
static class SmsApplicationListener {
@EventListener
public void listener(MyEvent event) {
log.debug("发送短信");
}
}
@Component
static class EmailApplicationListener {
@EventListener
public void listener(MyEvent event) {
log.debug("发送邮件");
}
}
@Bean
public ThreadPoolTaskExecutor executor(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(3);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(100);
return executor;
}
@Bean
public SimpleApplicationEventMulticaster applicationEventMulticaster(ThreadPoolTaskExecutor executor){
SimpleApplicationEventMulticaster multicaster = new SimpleApplicationEventMulticaster();
multicaster.setTaskExecutor(executor);
return multicaster;
}
定义自己注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyListener {
}
替换注解
@Component
static class SmsApplicationListener {
@MyListener
public void listener(MyEvent event) {
log.debug("发送短信");
}
}
@Component
static class EmailApplicationListener {
@MyListener
public void listener(MyEvent event) {
log.debug("发送邮件");
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A48_3.class);
SmsApplicationListener bean = context.getBean(SmsApplicationListener.class);
for (Method method : SmsApplicationListener.class.getMethods()) {
if(method.isAnnotationPresent(MyListener.class)){
ApplicationListener listener = new ApplicationListener() {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 监听器 方法需要的事件类型
System.out.println("事件---"+event);
Class<?> evenType = method.getParameterTypes()[0];//
if(evenType.isAssignableFrom(event.getClass())){
try {
method.invoke(bean,event);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
context.addApplicationListener(listener);
}
}
context.getBean(MyService.class).doBusiness();
context.close();
}
context.close 也会调用 ApplicationListener,所以反射的时候需要进行判断,不然会报错
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A48_3.class);
for (String name : context.getBeanDefinitionNames()) {
Object bean = context.getBean(name);
for (Method method : bean.getClass().getMethods()) {
if(method.isAnnotationPresent(MyListener.class)){
ApplicationListener listener = new ApplicationListener() {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 监听器 方法需要的事件类型
System.out.println("事件---"+event);
Class<?> evenType = method.getParameterTypes()[0];//
if(evenType.isAssignableFrom(event.getClass())){
try {
method.invoke(bean,event);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
context.addApplicationListener(listener);
}
}
}
context.getBean(MyService.class).doBusiness();
context.close();
}
利用SmartInitializingSingleton进行优化
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(A48_3.class);
context.getBean(MyService.class).doBusiness();
context.close();
}
-----------------
@Bean
public SmartInitializingSingleton smartInitializingSingleton(ConfigurableApplicationContext context){
return new SmartInitializingSingleton() {
@Override
public void afterSingletonsInstantiated() {
for (String name : context.getBeanDefinitionNames()) {
Object bean = context.getBean(name);
for (Method method : bean.getClass().getMethods()) {
if(method.isAnnotationPresent(MyListener.class)){
ApplicationListener listener = new ApplicationListener() {
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 监听器 方法需要的事件类型
System.out.println("事件---"+event);
Class<?> evenType = method.getParameterTypes()[0];//
if(evenType.isAssignableFrom(event.getClass())){
try {
method.invoke(bean,event);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
context.addApplicationListener(listener);
}
}
}
}
};
}