aspect spring

在Web应用程序开发期间,经常需要发送电子邮件。

但是,有时数据库中会包含来自生产的数据,并且存在在电子邮件测试执行期间向真实客户发送电子邮件的风险。

这篇文章将解释如何避免在没有在发送电子邮件功能中明确编写代码的情况下避免这种情况。

我们将使用两种技术:

  1. Spring Profiles –一种指示运行环境是什么的机制(即开发,生产,..)
  2. AOP –简而言之,它是一种以解耦的方式在方法上编写附加逻辑的机制。

我假设您已经在项目中设置了Profiles,并专注于Aspect方面。

在该示例中,发送电子邮件的类是EmailSender,其发送方法如下所示:

public class EmailSender {
//empty default constructor is a must due to AOP limitation
public EmailSender() {}//Sending email function
//EmailEntity - object which contains all data required for email sending (from, to, subject,..)
public void send(EmailEntity emailEntity) {
//logic to send email
}
}

现在,我们将添加防止在未在生产中运行代码的客户发送电子邮件的逻辑。
为此,我们将使用Aspects,这样我们就不必在send方法中编写它,从而可以保持关注点分离的原则。

创建一个将包含过滤方法的类:

@Aspect
@Component
public class EmailFilterAspect {public EmailFilterAspect() {}
}

然后创建一个PointCut来捕获send方法的执行:

@Pointcut("execution(public void com.mycompany.util.EmailSender.send(..))")public void sendEmail(){}

由于我们需要控制是否应执行该方法,因此需要使用Arround批注。

@Around("sendEmail()")
public void emailFilterAdvice(ProceedingJoinPoint proceedingJoinPoint){try {proceedingJoinPoint.proceed(); //The send email method execution} catch (Throwable e) {                           e.printStackTrace();}
}

最后一点,我们需要访问send方法的输入参数(即获取EmailEntity)并确认我们没有在开发中向客户发送电子邮件。

@Around("sendEmail()")public void emailFilterAdvice(ProceedingJoinPoint proceedingJoinPoint){//Get current profile
ProfileEnum profile = ApplicationContextProvider.getActiveProfile();Object[] args = proceedingJoinPoint.getArgs();        //get input parametersif (profile != ProfileEnum.PRODUCTION){//verify only internal mails are allowedfor (Object object : args) {if (object instanceof EmailEntity){String to = ((EmailEntity)object).getTo();if (to!=null && to.endsWith("@mycompany.com")){//If not internal mail - Dont' continue the method                        try {proceedingJoinPoint.proceed();} catch (Throwable e) {e.printStackTrace();}}}}}else{//In production don't restrict emailstry {proceedingJoinPoint.proceed();} catch (Throwable e) {e.printStackTrace();}}
}

而已。
关于配置,您需要在项目中包括纵横图罐。
在Maven中,它看起来像这样:

org.aspectjaspectjrt${org.aspectj.version}org.aspectjaspectjweaver${org.aspectj.version}runtime

在您的spring应用程序配置xml文件中,您需要具有以下内容:

祝好运!

参考:来自Gal Levinsky博客博客的JCG合作伙伴 Gal Levinsky 使用Aspect和Spring Profile进行电子邮件过滤 。

翻译自: https://www.javacodegeeks.com/2012/07/email-filtering-using-aspect-and-spring.html

aspect spring

aspect spring_使用Aspect和Spring Profile进行电子邮件过滤相关推荐

  1. 使用Aspect和Spring Profile进行电子邮件过滤

    在Web应用程序开发期间,经常需要发送电子邮件. 但是,有时数据库中会包含来自生产的数据,并且存在在电子邮件测试执行期间向真实客户发送电子邮件的风险. 这篇文章将解释如何避免在没有在发送电子邮件功能中 ...

  2. Springboot使用Maven Profile和Spring Profile进行多环境配置

    Springboot使用Maven Profile和Spring Profile进行多环境配置 目的 在实际的项目上,一般会分三种环境dev.test.prod来方便我们的开发和部署,要求我们在开发的 ...

  3. maven spring profile 协同

    请在网上查相关的使用情景,这里直接上要点.另外,可能不只一种方法,但这里只有一种. 1.POM.XML片段,使web.xml文件中有关活跃spring profile的内容可以被maven自动替换 & ...

  4. Spring Profile模式示例

    最近,我们介绍了Spring Profiles的概念. 此概念是针对不同部署环境的轻松配置区分符. 直接的用例(已提出)是对相关类进行注释,以便Spring根据活动的配置文件加载适当的类. 但是,这种 ...

  5. 【iOS】Scale Fill、Aspect Fit 和 Aspect Fill 的区别(Content Mode)

    ImageView -  Content Mode中Scale Fill.Aspect Fit 和 Aspect Fill 的区别: Scale Fill:缩放(改变图片比例)以填满整个Image V ...

  6. spring boot 构建docker镜像,运行指定spring profile

    #本文基于maven构建的spring boot项目,打包docker镜像需要安装docker 1.pom.xml添加docker打包插件 <plugin><groupId>c ...

  7. Springboot使用Maven项目使用 Profiles和Spring Profile进行多环境配置 动态激活指定

    最终效果:①:IDEA右边"maven"的工具栏 ②:项目配置的多环境选项入口[默认:dev] ③:项目配置的多环境配置文件 ④:选择②操作后编译生成的配置文件[去除其它环境配置文 ...

  8. Maven profile整合Spring profile

    在Maven和Spring中,都有profile这个概念.profile是用于区分各种环境的,例如开发环境.测试环境.正式环境等.Maven的profile用于在打包时根据指定环境替换不同环境的配置文 ...

  9. spring Profile

    前言 本文从如下3方面探讨Spring的Profile: Spring中的Profile是什么 为什么要使用Profile 如何使用Profile 1.Spring中的Profile 是什么? Spr ...

最新文章

  1. 中文分词的古今中外,你想知道的都在这里
  2. Python开发技巧-使用Python生成HTML表格
  3. 【英语学习】【Level 07】U07 Stories of my Life L1 Going to the Countryside
  4. np生成多维数组数组比较
  5. python编程100例-python100例,python经典例题
  6. nagios的check_tcp,check_udp插件的使用文档
  7. 1067 Sort with Swap(0, i) (25 分) 好,容易出错
  8. android暗水印技术,基于Android的隐藏数字水印技术的研究与实现
  9. 龙芯2k1000-pmon(7)- pmon版本控制
  10. javascript综合
  11. 利用“串口调试助手”等软件调试 PROTEUS 环境中 51单片机 的串行通信
  12. 联想T430 WIN8系统换WIN7系统的相关设置
  13. 【系统篇 / 域】❀ 06. Windows10 加入域 ❀ Windows Server 2016
  14. BeatSaber节奏光剑插件开发官方教程2-简单的插件示例
  15. 美丽的夕阳(小孩文章)
  16. qt样式表设置边框_QT样式表
  17. oracle常用函数number,Oracle 常用函数介绍
  18. 网格搜索法调参神经网络
  19. PC非正常断电,C盘开机自检
  20. 高新科技制造业招标采购改善解决方案

热门文章

  1. 什么是 CAS 机制
  2. C/C++输入输出流
  3. 新闻发布项目——接口类(commentDao)
  4. SpringCloudBus(了解)
  5. String转Double
  6. javah导出类的头文件抛出异常——java.lang.IllegalArgumentException: Not a valid class name(原因及解决方法)
  7. java 和javafx_Java,JavaFX的流利设计风格文本字段和密码字段
  8. 运动基元_开发人员的新分布式基元
  9. 五皇后问题 java_Java的5个古怪问题
  10. sql limit 子句_Java 8流中的常见SQL子句及其等效项