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

后端 未结 4 2001
小蘑菇
小蘑菇 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:37

    I would prefer to annotate your constructor with @Inject instead:

    @Stateless
    @LocalBean
    public class EmailService {
    
        ....
        @Deprecated
        public EmailService(){}       
    
        @Inject 
        public EmailService(ContextFinder ctf) { 
            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);
        }
    
    ...
    }
    

    >> Using @PostConstruct is not exactly the same as you had. init() method will not work outside the container like in JUnit test.

    You still need to provide the default not parametrized constructor (part of the The EJB 3.1 spec, section 4.9.2 Bean Classes).

提交回复
热议问题