使用Spring事件机制实现代码解耦

在项目开发过程中,代码耦合度越来越不利于代码的拓展与维护;使用spring事件机制来实现观察者模式来解耦各模块的代码调用;
逻辑图如下:
在这里插入图片描述
程序代码演示:
初始化顺序演示程序

pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<artifactId>springBean</artifactId>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<version>2.2.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
			<version>2.2.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
			<version>2.2.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<version>2.2.2.RELEASE</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

启动类

package springBean;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

import springBean.event.EventMsg;
import springBean.event.EventPublish;
/**
 * @EnableAsync 注解启用Async异步功能
 * @author eakom
 *
 */
@SpringBootApplication
@EnableAsync
public class MainBean {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(MainBean.class, args);
	}
	@Autowired
	EventPublish publish;
	/**
	 * 启动后执行
	 */
	@PostConstruct
	public void todo() {
		EventMsg msg=new EventMsg(this,"----消息内容");
		publish.publishEvent(msg);
	}
}

被观察者:业务执行的逻辑处理代码

package springBean.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
/**
 * 消息发布
 * @author admin
 *
 */
@Component
public class EventPublish implements ApplicationEventPublisher {
	@Autowired
	ApplicationEventPublisher applicationEventPublisher;
	@Override
	public void publishEvent(Object event) {
		// TODO Auto-generated method stub
		applicationEventPublisher.publishEvent(event);
	}

}

消息载体:

package springBean.event;

import org.springframework.context.ApplicationEvent;
/**
 * 事件实体(消息载体)
 * @author eakom
 *
 */
public class EventMsg extends ApplicationEvent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String msg;
	
	public EventMsg(Object source,String msg) {
		super(source);
		this.msg=msg;
		// TODO Auto-generated constructor stub
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}

观察者1(短信)

package springBean.event;

import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
 * 事件监听-短信
 * @author eakom
 * @Async 为异步执行,如果没有加@Async 为同步
 *
 */
@Component
@Async
public class EventListernPhone implements ApplicationListener<EventMsg>{

	@Override
	public void onApplicationEvent(EventMsg event) {
		String msg = event.getMsg();
		System.out.println("发送短信消息:"+msg);
	}

}

观察者1(微信)

package springBean.event;

import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
 * 事件监听-微信
 * @author eakom
 * @Async 为异步执行,如果没有加@Async 为同步
 *
 */
@Component
@Async
public class EventListernWeixin implements ApplicationListener<EventMsg>{

	@Override
	public void onApplicationEvent(EventMsg event) {
		String msg = event.getMsg();
		System.out.println("发送微信消息:"+msg);
	}

}

观察者1(邮件)

package springBean.event;

import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
 * 事件监听-邮件
 * @author eakom
 * @Async 为异步执行,如果没有加@Async 为同步
 *
 */
@Component
@Async
public class EventListernMail implements ApplicationListener<EventMsg>{

	@Override
	public void onApplicationEvent(EventMsg event) {
		String msg = event.getMsg();
		System.out.println("发送邮件消息:"+msg);
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值