【问题标题】:Inject a file using @Resource and JNDI in JEE6在 JEE6 中使用 @Resource 和 JNDI 注入文件
【发布时间】:2013-08-28 20:20:58
【问题描述】:

是否可以在 JEE6 中使用 JNDI 和 @Resource 注入文件?

如果是这样,我如何在 Glassfish 中设置 JNDI(文件)资源?

【问题讨论】:

    标签: glassfish java-ee-6 jndi


    【解决方案1】:

    如果您的目标是按如下方式配置属性文件:

    @Inject
    @Resource("META-INF/aws.properties")
    Properties awsProperties;
    

    那么你想使用 WELD 扩展,这在 WELD 文档here中有说明

    就像将它添加到您的 POM 一样简单

    <dependency>
       <groupId>org.jboss.weld</groupId>
          <artifactId>weld-extensions</artifactId>
          <version>${weld.extensions.version}</version>
          <type>pom</type>
          <scope>import</scope>
    </dependency>
    

    否则

    有关程序化方法,请参阅此article

    否则,

    将您的属性存储在数据库模式表中,并使用 JPA 2.0 使用指向您的 JNDI 的 JTA 检索它们。

    或者,如果您的应用程序是 JSF 应用程序:

    1. 在 faces-config.xml 文件中添加资源包,如下:

       <application>
          <resource-bundle>
              <base-name>/YourProperties</base-name>
              <var>yourProperties</var>
          </resource-bundle>
      </application>
      
    2. 在您的类路径或 maven 资源文件夹中添加相应的 YourProperties.properties 文件,如下所示:

    3. 在您的容器托管 bean 中添加以下 sn-p:

      private String someString;
      
      @PostConstruct
      public void loadProperty(){
          someString = ResourceBundle.getBundle("/YourProperties").getString("prop1");
       }
      

    【讨论】:

    • 你知道 Apache 的 CDI 实现 OpenWebbeans 中有什么类似的扩展吗?
    • 对不起,我没有。我很想知道您是如何在 Glassfish 中用 Open WebBeabs 替换 WELD 的?
    • 我没有,但我们的部署需要能够在 JEE 容器之间移动,而无需任何/大量更改。
    • 然后使用编程方式,因为JEE6没有注入属性文件的规范
    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多