0

(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

mostafa.S
  • 1,452
  • 4
  • 16
  • 27

1 Answers1

1

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.

Community
  • 1
  • 1
Gabriel Aramburu
  • 2,951
  • 2
  • 16
  • 20
  • Dear Gabriel, you mentioned two points; first of all thanks because I didn't know them; I applied them both this way: I defined EmailSenderService class with Stateless annotation to make it a bean class, and I removed the static keyword from the mailSession property, (check updates in the question); I republished my project to JBoss, but still the same; mailSession is null. I even tried PostConstruct for the method testMail() to make sure all the resource injections are done, but it’s null in that call as well. Any other idea? – mostafa.S Feb 09 '14 at 13:01
  • 1
    The problem is that your are using an instance created by yourself not by the Container: EmailSenderService emailSenderService = new EmailSenderService() this is a simple POJO that is not managed by the Container, takes a look at how a bean [works](http://docs.oracle.com/javaee/6/tutorial/doc/giplj.html) – Gabriel Aramburu Feb 09 '14 at 13:30
  • I checked your link and let the container instantiate the EmailSenderService class by @EJB, and the issue resolved. thanks – mostafa.S Feb 10 '14 at 06:34