@Resource injection in JBOSS AS 7 doesn't work

(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

Answers


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.


Need Your Help

Multi level view: expandablelistview

android treeview expandablelistview

I am trying to write multi level tree. I did look at expandablelistview, but it only supports two levels. I did look at other questions(SO) &amp; posts and the most promising post i have found is h...

How to stop users from spamming the submit button and sending multiple ajax requests?

jquery ajax

I have a from with an input which is disabled unless the textbox has text inside, but when a user adds text and smashes the submit button as fast as they can the ajax runs multiple times inserting ...