日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

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

發(fā)布時(shí)間:2023/12/3 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 aspect spring_使用Aspect和Spring Profile进行电子邮件过滤 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

aspect spring

在Web應(yīng)用程序開(kāi)發(fā)期間,經(jīng)常需要發(fā)送電子郵件。

但是,有時(shí)數(shù)據(jù)庫(kù)中會(huì)包含來(lái)自生產(chǎn)的數(shù)據(jù),并且存在在電子郵件測(cè)試執(zhí)行期間向真實(shí)客戶發(fā)送電子郵件的風(fēng)險(xiǎn)。

這篇文章將解釋如何避免在沒(méi)有在發(fā)送電子郵件功能中明確編寫(xiě)代碼的情況下避免這種情況。

我們將使用兩種技術(shù):

  • Spring Profiles –一種指示運(yùn)行環(huán)境是什么的機(jī)制(即開(kāi)發(fā),生產(chǎn),..)
  • AOP –簡(jiǎn)而言之,它是一種以解耦的方式在方法上編寫(xiě)附加邏輯的機(jī)制。
  • 我假設(shè)您已經(jīng)在項(xiàng)目中設(shè)置了Profiles,并專注于Aspect方面。

    在該示例中,發(fā)送電子郵件的類是EmailSender,其發(fā)送方法如下所示:

    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 } }

    現(xiàn)在,我們將添加防止在未在生產(chǎn)中運(yùn)行代碼的客戶發(fā)送電子郵件的邏輯。
    為此,我們將使用Aspects,這樣我們就不必在send方法中編寫(xiě)它,從而可以保持關(guān)注點(diǎn)分離的原則。

    創(chuàng)建一個(gè)將包含過(guò)濾方法的類:

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

    然后創(chuàng)建一個(gè)PointCut來(lái)捕獲send方法的執(zhí)行:

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

    由于我們需要控制是否應(yīng)執(zhí)行該方法,因此需要使用Arround批注。

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

    最后一點(diǎn),我們需要訪問(wèn)send方法的輸入?yún)?shù)(即獲取EmailEntity)并確認(rèn)我們沒(méi)有在開(kāi)發(fā)中向客戶發(fā)送電子郵件。

    @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();}} }

    而已。
    關(guān)于配置,您需要在項(xiàng)目中包括縱橫圖罐。
    在Maven中,它看起來(lái)像這樣:

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

    在您的spring應(yīng)用程序配置xml文件中,您需要具有以下內(nèi)容:

    祝好運(yùn)!

    參考:來(lái)自Gal Levinsky博客博客的JCG合作伙伴 Gal Levinsky 使用Aspect和Spring Profile進(jìn)行電子郵件過(guò)濾 。


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

    aspect spring

    總結(jié)

    以上是生活随笔為你收集整理的aspect spring_使用Aspect和Spring Profile进行电子邮件过滤的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。