简体   繁体   中英

Using @Resource to load environment entries

I'm trying to load bean runtime configuration.

@Stateless
public class MyBean implements MyLocal{    
   @Resource String runtimeSetting1="default_value";
   //....
}

I cannot find out how to create custom resource on app server side (Glassfish) - I have no idea what I should enter in "Factory Class" field. Maybe there is a better way of loading configuration...

Thanks.

To my knowledge, the standard way in Java EE is to declare env-entry for configuration data. This applies to all Java EE components like EJB 3 bean class, servlet, filters, interceptors, listener classes, etc. Here, declare something like this in your ejb-jar.xml :

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"> 
  <enterprise-beans>
    <session>
      <ejb-name>FooBean</ejb-name>
      <env-entry>
        <description>foobar entry</description>
        <env-entry-name>foo</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>bar</env-entry-value>
      </env-entry>
      ...
    </session>
    ...
  </enterprise-beans>
  ....
</ejb-jar>

Then look up the env-entry with JNDI or inject it by its name. For example, to inject it in your bean:

@Resource(name="foo")
private String myProperty; 

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