Skip to content

Commit

Permalink
feat: [功能]通知选择器通过 beanName+beanType 查找 Bean (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeswalker23 committed May 11, 2021
1 parent ca4238d commit db56c63
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import io.walkers.planes.fundhelper.entity.pojo.Response;
import io.walkers.planes.fundhelper.listener.DownloadFundValueEvent;
import io.walkers.planes.fundhelper.listener.RecalculateNullIncreaseRateEvent;
import io.walkers.planes.fundhelper.service.notice.NoticeMessage;
import io.walkers.planes.fundhelper.service.notice.NoticeMethod;
import io.walkers.planes.fundhelper.service.notice.NotificationSelector;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
Expand All @@ -29,8 +29,6 @@ public class CompensationController {
private FundDao fundDao;
@Resource
private ApplicationContext applicationContext;
@Resource
private NotificationSelector notificationSelector;

/**
* 拉取基金净值
Expand Down Expand Up @@ -58,21 +56,4 @@ public Response<String> recalculateNullIncreaseRate(@RequestParam("code") @NotBl
applicationContext.publishEvent(new RecalculateNullIncreaseRateEvent(this, fundModel));
return Response.success();
}

/**
* 发送邮件
*
* @return
*/
@Deprecated
@GetMapping("/sendMail")
public Response<String> sendMail() {
NoticeMessage noticeMessage = new NoticeMessage();
noticeMessage.setReceiver("fanyidong.fyd@alibaba-inc.com");
noticeMessage.setTitle("测试标题");
noticeMessage.setContent("测试内容");
noticeMessage.setNoticeMethod(NoticeMethod.Mail);
notificationSelector.doNotice(noticeMessage);
return Response.success();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public class NoticeMessage {
* 通知方式
* {@link NoticeMethod}
*/
private NoticeMethod noticeMethod;
private String noticeMethod;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
*
* @author 范逸东
*/
public enum NoticeMethod {
public interface NoticeMethod {

/**
* 邮件
*/
Mail,
String MAIL = "Mail";
/**
* 手机短信
*/
PhoneNote,
String PHONE_NOTE = "PhoneNote";
/**
* 手机电话
*/
PhoneCall;
String PHONE_CALL = "PhoneCall";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface Notification {
* @param noticeMethod 通知方式
* @return Boolean
*/
Boolean match(NoticeMethod noticeMethod);
Boolean match(String noticeMethod);

/**
* 通知
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author planeswalker23
*/
@Slf4j
@Service
@Service(value = NoticeMethod.MAIL)
public class NotificationByMail implements Notification {

@Value("${spring.mail.username}")
Expand All @@ -24,8 +24,8 @@ public class NotificationByMail implements Notification {
private JavaMailSender mailSender;

@Override
public Boolean match(NoticeMethod noticeMethod) {
return NoticeMethod.Mail.equals(noticeMethod);
public Boolean match(String noticeMethod) {
return NoticeMethod.MAIL.equals(noticeMethod);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
* 通知选择器,业务方使用该类进行通知
Expand All @@ -32,15 +31,15 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
* @return Boolean
*/
public Boolean doNotice(NoticeMessage noticeMessage) {
Map<String, Notification> notificationMap = applicationContext.getBeansOfType(Notification.class);
// notificationMap 返回不会为 null
for (Notification notification : notificationMap.values()) {
// 判断每一个通知 bean 是否与 NoticeMessage 的通知方式匹配
try {
Notification notification = applicationContext.getBean(noticeMessage.getNoticeMethod(), Notification.class);
if (notification.match(noticeMessage.getNoticeMethod())) {
return notification.notice(noticeMessage);
}
} catch (BeansException e) {
log.warn("通知方式[{}]非法,请检查", noticeMessage.getNoticeMethod());
log.error(e.getMessage());
}
log.warn("通知方式[{}]非法,请检查", noticeMessage.getNoticeMethod());
return Boolean.FALSE;
}
}

0 comments on commit db56c63

Please sign in to comment.