简体   繁体   中英

How to execute some code at EJB module startup

I have multiple EJB module, Each module need some default data in the database, the creation of some directories or files.

I suppose that if i create a @Startup @singleton EJB, i'll do the job, but it's the correct way? The ejb will stay in memory even if i use it only at startup

Yes, using a startup singleton bean is usually the correct way to do initializations, as @Startup doesn't work with @Stateless.

If you want to insert data into a database, another option would be to run an SQL script by the persistence.xml:

<property name="javax.persistence.sql-load-script-source"
      value="META-INF/sql/data.sql" />

This obviously has the drawback, that you can't manage the database inserts with Java code. For further information see here: http://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm

If you want to read more about EJBs (and singletons), see here: http://download.oracle.com/otn-pub/jcp/ejb-3.1-fr-eval-oth-JSpec/ejb-3_1-fr-spec.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM