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
答案 0 :(得分:1)
是的,使用启动单例bean通常是初始化的正确方法,因为@Startup不能与@Stateless一起使用。
如果要将数据插入数据库,另一个选项是通过persistence.xml运行SQL脚本:
<property name="javax.persistence.sql-load-script-source"
value="META-INF/sql/data.sql" />
这显然有一个缺点,即您无法使用Java代码管理数据库插入。有关详细信息,请参阅此处:http://docs.oracle.com/javaee/7/tutorial/persistence-intro005.htm
如果您想了解有关EJB(和单例)的更多信息,请参阅此处:http://download.oracle.com/otn-pub/jcp/ejb-3.1-fr-eval-oth-JSpec/ejb-3_1-fr-spec.pdf