'Bean没有公共构造函数不会参数'错误,尽管明显有一个? [英] 'Bean does not have a public constructor that does not take parameters' error despite clearly having one?

查看:184
本文介绍了'Bean没有公共构造函数不会参数'错误,尽管明显有一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EmailService EJB,它具有非常简单的'send_email'方法。我正在接收标题中的错误,尽管显然有一个没有参数的公共构造函数。以下是确切的错误和类代码。这是非常混乱的。



错误:


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




看到这里有错误的详细信息(不太可看): http://pic.dhe.ibm.com/infocenter/wxdinfo/v6r1/ index.jsp?topic =%2Fcom.ibm.websphere.messages.doc%2Fcom.ibm.ejs.container.container.html



代码:

  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;

/ **
*此类为邮件发送邮件提供了一个接口。它使用服务器环境变量来配置SMTP服务器和端口和身份验证。
*
* @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;
私人字符串服务器;
私有整数端口;


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

/ **
*向特定用户发送电子邮件。
*
* @param sendTo
* @param subject
* @param message
* /
public void sendMail(String sendTo,String subject,String消息){

try {
Email email = new SimpleEmail();
email.setHostName(server);
email.setSmtpPort(port);
email.setAuthentication(用户名,密码);
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()导致它是无错误的。

解决方案

为什么不将构造函数的初始化删除为@PostConstruct方法?

  @Stateless 
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;
私人字符串服务器;
私有整数端口;

@PostConstruct
public void init(){
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);
}

public void sendMail(String sendTo,String subject,String message){
//发送邮件
}

}


I have an EmailService EJB that has a very simple 'send_email' method. I'm receving the error in the title despite clearly having a public constructor that does not take parameters. Below is the exact error and the class code. This is very confusing.

Error:

[ERROR ] CNTR5007E: The websphere.jaxrs.service.EmailService bean class for the WebApiConsole#WebApiConsole.war#EmailService bean does not have a public constructor that does not take parameters.

See here for error details (not much to see): http://pic.dhe.ibm.com/infocenter/wxdinfo/v6r1/index.jsp?topic=%2Fcom.ibm.websphere.messages.doc%2Fcom.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();
            }

        }

    }

I'm inclined to think this is a bug. I might be wrong but but everything is pretty self-contained in the class above (no other configurations that I know of required) and I keep getting this error. I tried re-building the project.

[EDIT] I did some experimentation and very specifically, removing sendMail() causes it to be error free.

解决方案

Why you don't remove initialization from constructor into a @PostConstruct method?

@Stateless
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;

    @PostConstruct
    public void init() { 
        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);
    }

    public void sendMail(String sendTo, String subject, String message) {
        // send mail
    }

}

这篇关于'Bean没有公共构造函数不会参数'错误,尽管明显有一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆