Paste @Resource into a regular class

I am using glassfish v3, where I created a JavaMail session through the admin console. I want to use a Mail session as follows:

....
import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.*;

public class Mailer {

    MailGenerator mailGenerator;
    @Resource(name = "mail/WMCMail")
    private Session mailSession;

    public Mailer(MailGenerator mailGenerator) {
        this.mailGenerator = mailGenerator;
    }

    public void sendMixedMail(String recipient, String subject) {
        try {
            Message message = new MimeMessage(mailSession);

            message.setRecipients(
                Message.RecipientType.TO,
                InternetAddress.parse(recipient, false));
            message.setSubject(subject);

            ......

            Transport.send(message);

            logger.log(Level.INFO, "Mail sent to {0}.", recipient);
        } catch (MessagingException ex) {
            logger.log(Level.SEVERE, "Error in sending email to " + recipient, ex);
        } 
    }
}

When I call the sendMixedMail method, I see that mailSession is null. Is it impossible to insert a resource into a regular class? And when I say "normal", I mean a class that is not a managed bean or ejb-something.

+3
source share
1 answer

No, you cannot do this for a regular class. Quote from SUN J2EE Injection Page :

, Java EE 5 , , , EJB beans, JavaServer

. -, , , . -, , .

+5

Source: https://habr.com/ru/post/1779179/


All Articles