Question

(UPDATED)

I have deployed a web application on jboss-as-7.1.1.Final as standalone, and I have one startup class to initialize as below which works fine:

@Startup  
@Singleton  
public class StartupBean {  

    @PostConstruct  
    void init() {  
    EmailSenderService emailSenderService = new EmailSenderService();
    emailSenderService.testMail();
    }
}

the problem is in the other class defined as following:

@Stateless
public class EmailSenderService {

    @Resource(mappedName="java:jboss/mail/Default")
    private Session mailSession;

    @PostConstruct
    public void testMail(){
        if(mailSession == null){
            System.out.println("NULL"); 
        }
    }
}

when starting the application server, the method is called by the startup class, however the mailSession property is not initialized by the container and is null.

I have following configuration in my JBoss standalone.xml:

<subsystem xmlns="urn:jboss:domain:mail:1.0">
    <mail-session jndi-name="java:jboss/mail/Default">
        <smtp-server outbound-socket-binding-ref="mail-smtp">
        </smtp-server>
    </mail-session>
</subsystem>

Any Idea why mailSession is not initialized by the container?

Although this is a web application, I need to do some initialization prior to HTTP requests, that's why I'm using an @StartUp class and other stuff.

Regards

Était-ce utile?

La solution

The @Resource annotation must be used within a bean class. In your case the annotation defined in EmailSenderService it is just ignored by the EJB Container.

According to ejb 3.1 specification:

16.4.1.1Injection of Simple Environment Entries Using Annotations.

The Bean Provider uses the Resource annotation to annotate a field or method of the bean class as a target for the injection of a simple environment entry.

Also notice that the field must not be static.

16.2.2Annotations for Environment Entries.

The field or method may have any access qualifier (public, private, etc.) but must not be static.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top