0

Maybe this question could be duplicated. I just can't find any good search words.

How can I provide some development property value to SLSBs?

@LocalBean
@Stateless
class ClouldBean {

    public void doSomethingWithUsernameAndPassowrd() {
        // ...
    }

    private String username;
    private String password;
}

I just want to know how to inject username and password in a very standard and portable way.

Do I have to use some standard property configuration file?

Do I have to set it be contracted as being provided by container?

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

1 Answers1

2

Yes, there is standard and portable way and it is called environment entry.

Add following to ejb-jar.xml:

<ejb-jar>
    <enterprise-beans>
         ....
        <session>
            <ejb-name>ClouldBean</ejb-name>
         ....
            <env-entry>
               <env-entry-name>username</env-entry-name>
               <env-entry-type>java.lang.String</env-entry-type>
               <env-entry-value>my name</env-entry-value>
            </env-entry>
        ...
        </session>
    </enterprise-beans>
</ejb-jar>

And then you can inject value to the variable in your bean:

@Resource(name="username")
private String username;

For more detailed example you can take a look to this blog post. For all the details best source is EJB 3.1 specification section 16.4.

Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • Is it possible to modify these values in runtime? – ppapapetrou Jul 20 '12 at 06:34
  • In general no it is not. For reason ejb-jar.xml is called deployment descriptor. Of course some implementation can support such a functionality (as a accidental side effect or by purpose), but even in best case it is not portable behavior. – Mikko Maunu Jul 20 '12 at 09:31
  • Again, Ich bin Ihnen sehr dankbar. – Jin Kwon Jul 30 '12 at 13:11
  • I think you might be interested in [DeltaSpike](http://incubator.apache.org/projects/deltaspike.html) project or in some simple solution which you can provide by yourself like [this one](http://piotrnowicki.com/2012/06/inject-java-properties-in-java-ee-using-cdi/) – Piotr Nowicki Sep 19 '12 at 07:52