假设业务:查询快递状态,但不同的快递公司需要不同的参数。(有点离谱但这不重要)
1. 业务层
public interface ExpressService{
String selectExpressStatusByShunfeng();
String selectExpressStatusByYuantong();
String selectExpressStatusByZhongtong();
}
@Service(value = "EventExpressService")
public class ExpressServiceImpl implements ExpressService{
@Override
public String selectExpressStatusByShunfeng() {
SpringUtil.getApplicationContext().publishEvent(
new ShunfengEvent(this, (byte)1, (float)1.1, Boolean.TRUE));
return null;
}
@Override
public String selectExpressstatusByYuantong() {
Springutil.getApplicationContext().publishEvent(new YuantongEvent(
this, (short)10, 1.11, '\u0031'));
return null;
}
@Override
public String selectExpressstatusByZhongtong() {
// 懒得做了
return null;
}
}
2. Spring容器类
第一种方式
@Component
public class SpringUtil implements ApplicationContextAware {
/** spring上下文 */
private static ApplicationContext applicationcontext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throw BeansException {
SpringUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
第二种方式
@Component
public class SpringUtil implements ApplicationEventPublisherAware {
private static ApplicationEventPublisher applicationEventPublisher;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher application) {
SpringUtil.applicationEventPublisher = applicationEventPublisher;
}
public static ApplicationEventPublisher getApplicationEventPublisher() {
return applicationEventPublisher;
}
}
3. 三个快递公司需要的三种实体类
@Data
public class ShunfengEvent extends ApplicationEvent {
private Byte shunfeng1;
private Float shunfeng2;
private Boolean shunfeng3;
public ShunfengEvent(Object source,
Byte shunfeng1,
Float shunfeng2,
Boolean shunfeng3){
super(source);
this.shunfeng1 = shunfeng1;
this.shunfeng2 = shunfeng2;
this.shunfeng3 = shunfeng3;
}
}
@Data
public class ShunfengEvent extends ApplicationEvent {
private Short yuantong1;
private Double yuantong2;
private Character yuantong3;
public YuantongEvent(Object source,
Short yuantong1,
Double yuantong2,
Character yuantong3){
super(source);
this.yuantong1 = yuantong1;
this.yuantong2 = yuantong2;
this.yuantong3 = yuantong3;
}
}
// 懒得做了
public class ZhongtongEvent {
private Integer zhongtong1;
private BigDecimal zhongtong2;
private String zhongtong3;
}
4. 三个监听类
@Component
public class ShunfengExpressListener {
@EventListener
public void shunfengEvent (ShunfengEvent event) {
System.out.println(event.getShunfeng1() + "+" + event.getShunfeng2().toString + "+" + event.getShunfeng3() + "+" + event.getSource().toString());
}
}
@Component
public class YuantongExpressListener {
@EventListener
public void yuantongEvent (YuantongEvent event) {
System.out.println(event.getYuantong1() + "+" + event.getYuantong2() + "+" + event.getYuantong3() + "+" + event.getSource());
}
}
@Component
public class ZhongtongExpressListener {
@EventListener
public void zhongtongEvent (ZhongtongEvent event) {
System.out.println("继续懒得做,然后因为实体类没继承ApplicationEvent,调用发送时会报空指针异常");
}
}
5. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class EventExpressTest {
@Autowired
private ExpressService expressService;
@Test
public void test(){
expressService.selectExpressStatusByShunfeng();
expressService.selectExpressStatusByYuantong();
}
}