如何在GlassFish 4中修改Web环境条目

Mar*_*ila 11 java jndi glassfish-4

在我的web.xmlOD我的webapp应用我有以下因素:

<env-entry>
    <env-entry-name>aMessage</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>Hello World</env-entry-value>
</env-entry>
Run Code Online (Sandbox Code Playgroud)

此Web应用程序中的EJB可以读取它:

final InitialContext context = new InitialContext();
final Context env = (Context) context.lookup("java:comp/env");
System.out.println("MSG: " + env.lookup("aMessage")); // prints Hello World
Run Code Online (Sandbox Code Playgroud)

现在我试图通过以下方式更改该值asadmin:

martin@bono:~/glassfish4/glassfish/bin$ ./asadmin set-web-env-entry --name=aMessage --value=test webapp
Previous env-entry setting of aMessage for application/module webapp was overridden.
Command set-web-env-entry executed successfully.

martin@bono:~/glassfish4/glassfish/bin$ ./asadmin list-web-env-entry webapp
Reported 1 env-entry setting
aMessage (java.lang.String) = test ignoreDescriptorItem=true //
Command list-web-env-entry executed successfully.
Run Code Online (Sandbox Code Playgroud)

不幸的是,即使重新启用此Web应用程序或重新启动Web服务器,我的EJB仍会打印旧值"Hello World".

我也尝试过set-web-env-entry没有定义的名字web.xml,也玩过--ignoredescriptoritem参数,但没有任何帮助.枚举整个环境也不会显示任何其他或更改的Web环境条目,但会显示旧的以及与此问题无关的许多其他对象:

final NamingEnumeration<Binding> enu = env.listBindings("");

while (enu.hasMore()) {
    final Binding binding = enu.next();
    System.out.println(binding);
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

小智 0

这似乎是一个错误 - 但我有另一个解决方案可以满足您的需求。您可以使用 glassfish 中提供的自定义资源。您必须在domain.xml中声明自定义资源

<resources>
    <custom-resource factory-class="org.glassfish.resources.custom.factory.PropertiesFactory" res-type="java.util.Properties" jndi-name="test/properties">
      <property name="aMessage" value="Hello World"></property>
    </custom-resource>
</resources>
Run Code Online (Sandbox Code Playgroud)

然后你可以在代码中使用它



public class Environment
{

  public String getProperty() {

      InitialContext ctx = new InitialContext();
      properties = (Properties) ctx.lookup("test/properties");
      if(properties == null) {
          return "default value - hello";
      }

      return properties.getProperty("aMessage");    

  }

}
Run Code Online (Sandbox Code Playgroud)

这种方法的一个缺点是自定义资源对于整个域来说是全局的。但此解决方案的优点是您还可以使用 asadmin 和 admin Web 控制台更改资源。