wap手机关键词优化的软件
aop 切入
类名 ExceptionAspect
/**
* 切入点配置
*/
@within =切入所有的类包括子类注解
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
//出现异常之后进入此类
@AfterThrowing(value =“restControllerPointCut()”,throwing = “e”)
主要学习内容
通过 代码
@AllArgsConstructor
public class RobotSendException implements ISendException {/*** 允许多端发送*/private List<ISendMessage> sendMessageList;private IErrorMessageHandler errorMessageHandler;@Overridepublic boolean send(JoinPoint joinPoint, Exception e) {try {String message = errorMessageHandler.message(joinPoint, e);for (ISendMessage sendMessage : sendMessageList) {sendMessage.send(message);}return true;} catch (Throwable t) {/*** 捕获可能异常,切面记录日志*/return false;}}
}
其中主要 依赖两个注入
private List sendMessageList;
private IErrorMessageHandler errorMessageHandler;
作用
sendMessageList 通过这个 类进行信息发送
errorMessageHandler 处理统一的异常
其中 List 是通过
RobotProperties 配合 RobotAutoConfiguration
通过@ConditionalOnProperty注解判断是否存在 运用@ConditionalOnMissingBean来注入该类
结合spring spi 机制 配置
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
com.aizuda.robot.autoconfigure.RobotAutoConfiguration
若yml 配置了则会成功注入
其中主要方法是
ISendMessage.send
该接口由于 抽象类 AbstractRobotSendMessage 实现 并通过 方法 request 动态处理 url
restTemplate.postForEntity(this.getUrl(),new HttpEntity<>(objectMap, headers), Map.class).getBody();/*** 请求发送地址** @throws Exception*/public abstract String getUrl() throws Exception;
其他多个发送信息类 各继承 AbstractRobotSendMessage
实现 send 和 getUrl
在send 中 调用url 第二个参数 则 传入发送的值 而在 AbstractRobotSendMessage .request 中调用 this.getUrl 也在 各个类中进行重写
@Overridepublic boolean send(String message) throws Exception {return this.request(restTemplate, new HashMap<String, Object>(3) {{put("msgtype", "text");put("at", new HashMap<String, Object>(1) {{put("isAtAll", true);}});put("text", new HashMap<String, Object>(1) {{put("content", message);}});}});}@Overridepublic String getUrl() throws Exception {RobotProperties.DingTalk dingTalk = robotProperties.getDingTalk();StringBuffer url = new StringBuffer();url.append("https://oapi.dingtalk.com/robot/send?access_token=");url.append(dingTalk.getAccessToken());String secret = dingTalk.getSecret();if (StringUtils.hasLength(secret)) {Long timestamp = System.currentTimeMillis();url.append("×tamp=").append(timestamp);String sign = AlgorithmUtils.encodeBase64HmacSHA256(secret, timestamp + "\n" + secret);url.append("&sign=").append(URLEncoder.encode(sign, "UTF-8"));}return url.toString();}
从而实现方法服用
