有没有一个漂亮的解决方案来禁用所有电子邮件通知?

我有一个邮件服务,它只是向收件人发送通知。


@Service

public class MailService {


        @Autowired

        private JavaMailSender mailSender;


        public void send(String subject, String text, String... emails) {

                // MailMessage object configuration

                mailSender.send(mail);

        }

}

然后我有一个休息控制器,在一些业务逻辑之后调用这个方法。


@RestController

public class MyRestController {


        @Autowired

        private MailService mailService;


        @PostMapping("/orders")

        public void createOrders(@RequestBody List<Order> orders) {

                // manipulations with orders

                mailService.send("Order notification", "New orders", "1@mail.com");

        }

}

我应该如何设计一个应用程序来关闭不同弹簧配置文件的通知?是否有根据配置文件配置电子邮件分发的最佳实践?


Cats萌萌
浏览 135回答 3
3回答

慕姐8265434

使用多种实现一种可能性是创建MailService具有两个实现的接口。通过将这些实现与@Profile注释相结合,您可以根据您使用的配置文件正确注入一种或另一种实现。例如:public interface MailService {&nbsp; &nbsp; void send(String subject, String text, String... emails);}然后您可以将@Profile("mail")注释添加到一个实现中:@Service@Profile("mail") // Only create this bean when the 'mail' profile is usedpublic class JavaMailServiceImpl implements MailService {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private JavaMailSender mailSender;&nbsp; &nbsp; public void send(String subject, String text, String... emails) {&nbsp; &nbsp; &nbsp; &nbsp; // MailMessage object configuration&nbsp; &nbsp; &nbsp; &nbsp; mailSender.send(mail);&nbsp; &nbsp; }}此外,您可以在邮件配置文件未激活时添加另一个实现:@Service@Profile("!mail") // By adding the exclamination mark, this implementation will be used when the mail profile isn't activepublic class NoopMailServiceImpl implements MailService {&nbsp; &nbsp; private final Logger logger = LoggerFactory.getLogger(getClass());&nbsp; &nbsp; @Override&nbsp; &nbsp; public void send(String subject, String text, String... emails) {&nbsp; &nbsp; &nbsp; &nbsp; logger.debug("Dummy implementation, no e-mail is being sent");&nbsp; &nbsp; }}如果您现在自动连接MailService到控制器中,它将根据您使用的配置文件正确实现正确的实现。使用方面另一种替代方法是使用方面将发送邮件与订单创建逻辑完全解耦。这允许您完全删除控制器MailService的依赖关系MyRestController:@RestControllerpublic class MyRestController {&nbsp; &nbsp; @PostMapping("/orders")&nbsp; &nbsp; public void createOrders(@RequestBody List<Order> orders) {&nbsp; &nbsp; &nbsp; &nbsp; // manipulations with orders&nbsp; &nbsp; }}相反,您可以将mailService逻辑添加到单独的方面:@Aspect@Component@Profile("mail")public class OrderNotificationAspect {&nbsp; &nbsp; private final MailService mailService;&nbsp; &nbsp; public OrderNotificationAspect(MailService mailService) {&nbsp; &nbsp; &nbsp; &nbsp; this.mailService = mailService;&nbsp; &nbsp; }&nbsp; &nbsp; @AfterReturning("execution(* com.example.MyRestController.createOrders(..))")&nbsp; &nbsp; public void sendNotification() {&nbsp; &nbsp; &nbsp; &nbsp; mailService.send("Order notification", "New orders", "1@mail.com");&nbsp; &nbsp; }}像以前一样,我们使用@Profile注释仅在邮件配置文件处于活动状态时注册方面 bean。然而,由于我们不再直接将控制器绑定到MailService我们不再需要“虚拟实现”。要在 Spring boot 中使用切面,您应该添加spring-boot-starter-aop依赖项。以编程方式检查配置文件另一种选择是在代码中编写检查,例如:@RestControllerpublic class MyRestController {&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private MailService mailService;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private Environment environment;&nbsp;&nbsp;&nbsp; &nbsp; @PostMapping("/orders")&nbsp; &nbsp; public void createOrders(@RequestBody List<Order> orders) {&nbsp; &nbsp; &nbsp; &nbsp; // manipulations with orders&nbsp; &nbsp; &nbsp; &nbsp; if (environment.acceptsProfiles(Profiles.of("mail"))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mailService.send("Order notification", "New orders", "1@mail.com");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}根据您希望此逻辑的细粒度程度,您可以将其放入控制器或服务中(如果您想禁用为特定配置文件发送的所有电子邮件)。您选择哪个选项取决于您的需求。使用多种实现是禁用所有邮件发送的最简单方法。但是,如果您只想禁用某些邮件的发送,则必须选择其他方式。使用方面的缺点是它有时会使流程更难以遵循,尽管 IDE 对方面有很好的支持。

有只小跳蛙

只需为需要它的配置文件提供一个虚拟实现@Bean@Profile("prod")public JavaMailSender getRealJavaMailSender() {&nbsp; &nbsp; JavaMailSenderImpl mailSender = new JavaMailSenderImpl();&nbsp; &nbsp; mailSender.setHost("smtp.gmail.com");&nbsp; &nbsp; mailSender.setPort(587);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; mailSender.setUsername("my.gmail@gmail.com");&nbsp; &nbsp; mailSender.setPassword("password");&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; Properties props = mailSender.getJavaMailProperties();&nbsp; &nbsp; props.put("mail.transport.protocol", "smtp");&nbsp; &nbsp; props.put("mail.smtp.auth", "true");&nbsp; &nbsp; props.put("mail.smtp.starttls.enable", "true");&nbsp; &nbsp; props.put("mail.debug", "true");&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; return mailSender;}@Bean@Profile("test")public JavaMailSender getDummyJavaMailSender() {&nbsp; &nbsp; return new JavaMailSender() {&nbsp; &nbsp; &nbsp; &nbsp; ... dummy method implementations ...&nbsp; &nbsp; };}

慕尼黑8549860

sendEmails=false我建议向 application-[profile-name].properties 文件添加一个变量(例如),每个配置文件具有不同的设置。然后,在您的代码中,您可以使用此属性来决定是否应发送电子邮件。@Servicepublic class MailService {&nbsp; &nbsp; @Value("sendEmails}")&nbsp; &nbsp; private boolean sendEmails;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; private JavaMailSender mailSender;&nbsp; &nbsp; public void send(String subject, String text, String... emails) {&nbsp; &nbsp; &nbsp; &nbsp; if (sendEmails) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // MailMessage object configuration&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mailSender.send(mail);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}这种方法使您能够为每个配置文件使用不同的(且易于更改的)设置。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java