English 中文(简体)
@Singleton bean改为ejb-jar.xml
原标题:@Singleton bean to read values from ejb-jar.xml

页: 1 SLSB, 读到ejb-jar.xml文档中的配置值。

This class has only private fields and for each field a corresponding getter method. In my application I have a lot of other EJBs that use an instance of ConfigParameter to read the configuration values.

Now, I thought it would be good idea to make ConfigParameter a @Singleton, since its state is shared across the whole application.

但在<代码>ejb-jar.xml 仅限<条码>、<条码>或<条码>。 档案对此有目共 The:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" version="3.0">
  <enterprise-beans>    
    <session>
      <ejb-name>ConfigParameter</ejb-name>
      <ejb-class>myapp.util.ConfigParameter</ejb-class>      
      <session-type>Stateless</session-type> <!-- make this a Singleton -->
      <env-entry>
        <env-entry-name>adminRoleName</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>admin</env-entry-value>
      </env-entry>
    </session>
  </enterprise-beans>
</ejb-jar>

我的问题是:

  1. Is it a good idea to make this bean a singleton?
  2. If yes, how can I achieve this?
  3. For understanding: Why is it not allowed to set session-type to Singleton?
最佳回答

页: 1 在EJB3.1中引入了单一州EJB。 如果你想在ejb-jar.xml中加以界定,那么你应界定适当的版本(注version属性,XSD所在地改为3.1)。

<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" 
    version="3.1">

Or just use @Singleton annotation on the EJB class itself.

是的——我肯定地说,it s appropriate,以标注整个应用配置的星号为单一州。

问题回答

While the Singleton EJB is a good option, I d like to propose another idea for consideration. Depending on how many values you need and where they are used, this may or may not be a better approach.

自<条码> Unton EJB只是一个POJO,通过@Resource annotation从JNDI中收回其价值,但可以使用模块或应用范围的环境条目(java:module,java:app)。 这可以消除对这一额外EJB类的需求;相反,你只能使用每个需要这些价值的EJBs内部的@Resource。 为此,仅使用在EJB/web部分中需要该价值的“:modulejava:app:<>>:app:@Resource(name=“java:app. >)>上预先确定的名称。





相关问题
Recommended way to develop using Jetty and Eclipse

I am currently developing a J2EE application and I would like to use Jetty. I would like to have iot integrated with Eclipse, so I could debug the appliaction. I ve tried out couple of plugins (...

Call function periodically in Java

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server ...

Why make an EJB rather than a Web Service?

I would have thought that there is a lot of information out there on this, but I haven t found anything that really answers my question. What are the advantages of making an EJB rather than a web ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

JNDI Names -- Is Prefix "jdbc/" needed?

What s up with JNDI names? I m trying to get a javax.sql.DataSource using the new annotations feature of Java 5. It s not working for me, so I want to ask... I have a in my web.xml, inside of it is ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...