Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

glassfish - Inject a file using @Resource and JNDI in JEE6

Is it possible to inject a file using JNDI and @Resource in JEE6?

If so how do I setup and JNDI (file) resource in Glassfish?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your objective is to configure a properties file as follows:

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

then you want to use a WELD extension which is explained in the WELD documentation here

It is as simple as adding this to your POM

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

Otherwise

See this article for a programmatic approach.

Or else,

Store your properties in a DB schema table and use JPA 2.0 to retrieve them using JTA pointing to your JNDI.

Or if your application is a JSF one:

  1. Add a resource bundle in the faces-config.xml file as follows:

     <application>
        <resource-bundle>
            <base-name>/YourProperties</base-name>
            <var>yourProperties</var>
        </resource-bundle>
    </application>
    
  2. Add the corresponding YourProperties.properties file in your classpath or maven resources folder as follows:
    Maven resource folder

  3. In your container managed bean add the following snippet:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...