Reputation: 6153
I have a ConfigParameter
SLSB that reads configuration values from the ejb-jar.xml
file.
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.
But in the ejb-jar.xml
I can set session-type
only to Stateful
or Stateless
. The file looks like this:
<?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>
My questions are:
session-type
to Singleton?Upvotes: 0
Views: 4202
Reputation: 7324
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.
Since your ConfigParameter
Singleton EJB is just a POJO that retrieves its values from JNDI via the @Resource
annotation, it may be possible for you to use module or application-scoped environment entries (java:module
, java:app
) instead. This could eliminate the need for this extra EJB class; instead, you can just use the @Resource
annotation within each of the EJBs that needs the values. To do this, just use the @Resource
annotation within the EJB/web component that requires the value, using a name prefixed with either java:module
or java:app
: @Resource(name="java:app...")
.
Upvotes: 0
Reputation: 18174
You're using EJBs 3.0. Singleton EJBs were introduced in EJB 3.1. If you want to define it in ejb-jar.xml you should define appropriate version (note the version attribute and XSD location changed to 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.
And yes - I would definitely say that it's appropriate to mark application-wide configuration bean as Singleton.
Upvotes: 3