Startup bean not called
Asked Answered
A

4

7

I created a Java Web Application Project in NetBeans, and created a startup bean in it:

package malibu.util;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

@Stateless
@LocalBean
@javax.ejb.Startup
public class Startup {
    @EJB
    private ProviderEJB providerEJB;

    @PostConstruct
    public void onStartup() {
        System.err.println("Initialization success.");
    }
}

But the code is not called after I deploy the application. What can cause this?

Antedate answered 25/7, 2011 at 18:48 Comment(0)
W
12

Try the following set of annotations:

@Singleton
@Startup
public class Startup {
    @EJB
    private ProviderEJB providerEJB;

    @PostConstruct
    public void onStartup() {
        System.err.println("Initialization success.");
    }
}

You will find more details here and in this book (chapter 2).

Wundt answered 25/7, 2011 at 18:59 Comment(0)
L
7

The Startup annotation is for usage with Singleton beans, not with stateless beans. See the javadoc.

Also, @LocalBean is not needed in this case. This declares that you want an additional no-interface view, but this is only needed if the bean implements a remote or local business interface. If you omit it you get a no-interface view by default.

Laurinda answered 25/7, 2011 at 18:59 Comment(0)
O
1

In my case JBoss 7EAP required the ejb-jar.xml config file on the war to load the @Startup EJB .

<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
           version="3.1">
 <enterprise-beans>...</enterprise-beans></jboss:ejb-jar>
Oxidation answered 28/2, 2020 at 20:57 Comment(0)
N
0

http://docs.oracle.com/javaee/6/api/javax/ejb/Startup.html

Mark a singleton bean for eager initialization during the application startup sequence.

Noise answered 5/8, 2014 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.