在JEE6中使用@Resource和JNDI注入文件 [英] Inject a file using @Resource and JNDI in JEE6
问题描述
是否可以在JEE6中使用JNDI和@Resource注入文件?
如果是这样,我如何在Glassfish中设置和JNDI(文件)资源?如果你的目标是配置一个属性文件,如下所示:
@Inject
@Resource(META-INF / aws.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>
< /依赖关系>
其他
请参阅文章的程序化方法。 p>
否则,
将您的属性存储在数据库模式表中,并使用JPA 2.0使用JTA指向你的JNDI。
或者如果您的应用程序是JSF应用程序:
-
添加如下所示的faces-config.xml文件中的资源包:
< application>
< resource-bundle>
< base-name> / YourProperties< / base-name>
< var> yourProperties< / var>
< / resource-bundle>
< / application>
-
在您的classpath或maven资源文件夹中添加相应的YourProperties.properties文件,如下所示:
- 在您的容器托管bean中添加以下代码片段:
private String someString;
@PostConstruct
public void loadProperty(){
someString = ResourceBundle.getBundle(/ YourProperties)。getString(prop1);
}
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?
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:
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>
Add the corresponding YourProperties.properties file in your classpath or maven resources folder as follows:
In your container managed bean add the following snippet:
private String someString; @PostConstruct public void loadProperty(){ someString = ResourceBundle.getBundle("/YourProperties").getString("prop1"); }
这篇关于在JEE6中使用@Resource和JNDI注入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!