'Bean does not have a public constructor that does not take parameters' error despite clearly having one?

后端 未结 4 2047
小蘑菇
小蘑菇 2021-01-24 07:21

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 para

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-24 07:57

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

提交回复
热议问题