尽管明显有一个错误,但“Bean 没有不带参数的公共构造函数”错误?

2023-12-01

我有一个 EmailService EJB,它有一个非常简单的“send_email”方法。尽管显然有一个不带参数的公共构造函数,但我还是收到了标题中的错误。下面是确切的错误和类代码。这很令人困惑。

Error:

[错误] CNTR5007E:websphere.jaxrs.service.EmailService bean WebApiConsole#WebApiConsole.war#EmailService bean 的类 没有不带参数的公共构造函数。

有关错误详细信息,请参阅此处(没什么可看的):http://pic.dhe.ibm.com/infocenter/wxdinfo/v6r1/index.jsp?topic=%2f Com.ibm.websphere.messages.doc%2f Com.ibm.ejs.container.container.html

Code:

package websphere.jaxrs.service;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import websphere.jaxrs.helpers.ContextFinder;

    /**
     * This class provides an interface for emailing messages. It uses server environment variables to configure SMTP server and port and authentication.
     * 
     * @author me
     *
     */
    @Stateless
    @LocalBean
    public class EmailService {

        @EJB ContextFinder ctf;

        private static final String EMAIL_PASSWORD_JNDI_NAME = "EMAIL_PASSWORD";
        private static final String EMAIL_USERNAME_JNDI_NAME = "EMAIL_USERNAME";
        private static final String SMTP_SERVER_JNDI_NAME = "SMTP_SERVER";
        private static final String SMTP_PORT_JNDI_NAME = "SMTP_PORT";

        private String username;
        private String password;
        private String server;
        private Integer port;


        public EmailService() { 
            username = (String) ctf.lookup(EMAIL_USERNAME_JNDI_NAME);
            password = (String) ctf.lookup(EMAIL_PASSWORD_JNDI_NAME);
            server = (String) ctf.lookup(SMTP_SERVER_JNDI_NAME);
            port = (Integer) ctf.lookup(SMTP_PORT_JNDI_NAME);
        }

        /**
         * Sends an email to a specific user.
         * 
         * @param sendTo
         * @param subject
         * @param message
         */
        public void sendMail(String sendTo, String subject, String message) {

            try {
                Email email = new SimpleEmail();
                email.setHostName(server);
                email.setSmtpPort(port);
                email.setAuthentication(username, password);
                email.setSSLOnConnect(true);
                email.setFrom(username);
                email.setSubject(subject);
                email.setMsg(message);
                email.addTo(sendTo);
                email.send();
            } catch (EmailException e) {
                System.err.println("Failed to email");
                e.printStackTrace();
            }

        }

    }

我倾向于认为这是一个错误。我可能是错的,但上面的类中的所有内容都非常独立(我知道不需要其他配置),并且我不断收到此错误。我尝试重新构建该项目。

[编辑] 我做了一些实验,非常具体地,删除 sendMail() 会导致它没有错误。


注射尚未发生。

简而言之:

  1. 容器调用构造函数
  2. 然后注入的字段被注入
  3. 然后后构造的事情发生了

因此,按照建议,将查找提取到后构造方法中

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尽管明显有一个错误,但“Bean 没有不带参数的公共构造函数”错误? 的相关文章

随机推荐