@Resource injection in JBOSS AS 7 doesn't work


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:

public class StartupBean {  

    void init() {  
    EmailSenderService emailSenderService = new EmailSenderService();

the problem is in the other class defined as following:

public class EmailSenderService {

    private Session mailSession;

    public void testMail(){
        if(mailSession == 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">

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.



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: 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 ...