【问题标题】:How to call a method which is inside another classes?如何调用另一个类中的方法?
【发布时间】:2017-08-29 08:59:27
【问题描述】:

我有这个方法如下所示。我怎样才能在某个地方调用这个方法?由于另一个内部有一个方法,我不清楚我应该调用外部方法还是内部方法。

像这样,

sendConfirmationEmail(user);

像这样,

prepare(mimeMessage);

在第二种情况下,我是否也需要处理异常。由于我对 Java 不太熟悉,所以我不太明白。

我正在创建一个 Spring Web 应用程序。我想从 Java 控制器调用它。请帮忙。

public void sendConfirmationEmail(final User user) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setTo("sanduni@vclhq.com");
            //message.setFrom("adbuylk@gmail.com");
            message.setFrom(new InternetAddress("adbuylk@gmail.com"));
            Map model = new HashMap();
            model.put("user", user);
            String text = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, "com/dns/registration-confirmation.vm", model);
            message.setText(text, true);
        }
    };
    this.mailSender.send(preparator);
}

【问题讨论】:

标签: java spring rest vector


【解决方案1】:

在Spring中,如果你想调用另一个类的方法,你可以连接需要的类,并且可以调用另一个类的方法。

【讨论】:

    【解决方案2】:

    首先在要调用方法的地方自动装配服务,然后使用自动装配的对象调用函数

    @Autowired
     private EmailService emailService;
    

    ...........

    emailService.sendConfirmationEmail();
    
    ................
    

    【讨论】:

      【解决方案3】:

      您可以将此代码复制到您的服务实现中,然后您可以调用您的方法

      【讨论】:

      • 这是在服务中。我想知道如何调用方法?
      • 也可以设为静态,直接调用即可
      • 是的,该怎么做
      • 在你的控制器中使用@Autowire(依赖注入)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      相关资源
      最近更新 更多