开发者

Startup bean not called

开发者 https://www.devze.com 2023-03-22 23:48 出处:网络
I created a Java Web Application Project in NetBeans, and created a startup bean in it: package malibu.util;

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 Pr开发者_运维百科oviderEJB 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?


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


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.


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>


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号