EJB3.1属性文件注入

发布于 2024-11-26 12:16:29 字数 230 浏览 0 评论 0原文

有没有一些简单的方法可以将加载有文件的 Properties 类从类路径注入到 EJB (3.1) 中?

像这样的:

@Resource(name="filename.properties", loader=some.properties.loader)
private Properties someProperties;

谢谢你,

Bozo

is there some simple way to inject Properties class loaded with a file from the classpath into EJB (3.1)?

Something like this:

@Resource(name="filename.properties", loader=some.properties.loader)
private Properties someProperties;

Thank you,

Bozo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2024-12-03 12:16:29

正如 bkail 所说,您可以通过以下方式实现这一目标。我不确定您的 loader=some.properties.loader 的真正含义,因此跳过了执行任何操作,但提供了选项,以防您想使用 loader.getClass( ).getResourceAsStream("filename.properties");

首先定义您的注入类型

@BindingType
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER })
public @interface PropertiesResource {

    @Nonbinding
    public String name();

    @Nonbinding
    public String loader();

}

,然后为其创建一个生产者

public class PropertiesResourceLoader {

    @Produces
    @PropertiesResource(name = "", loader = "")
    Properties loadProperties(InjectionPoint ip) {
        System.out.println("-- called PropertiesResource loader");
        PropertiesResource annotation = ip.getAnnotated().getAnnotation(
                PropertiesResource.class);
        String fileName = annotation.name();
        String loader = annotation.loader();
        Properties props = null;
        // Load the properties from file
        URL url = null;
        url = Thread.currentThread().getContextClassLoader()
                .getResource(fileName);
        if (url != null) {
            props = new Properties();
            try {
                props.load(url.openStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return props;
    }
}

,然后将其注入到您的命名组件中。

@Inject
@PropertiesResource(name = "filename.properties", loader = "")
private Properties props;

我这样做是为了查看焊接文档,其中以 @HttpParam 为例此处。这是根据焊接1.1.0,在焊接1.0.0中,获取注释可以这样完成

PropertiesResource annotation = ip.getAnnotation(PropertiesResource.class);

As bkail said, you can achieve this in the following way. I am not sure what your loader=some.properties.loader really meant, so skipped doing anything with that, but provided option for that in case you want to load using the loader.getClass().getResourceAsStream ("filename.properties");

First define your injection type

@BindingType
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
        ElementType.PARAMETER })
public @interface PropertiesResource {

    @Nonbinding
    public String name();

    @Nonbinding
    public String loader();

}

Then create a producer for that

public class PropertiesResourceLoader {

    @Produces
    @PropertiesResource(name = "", loader = "")
    Properties loadProperties(InjectionPoint ip) {
        System.out.println("-- called PropertiesResource loader");
        PropertiesResource annotation = ip.getAnnotated().getAnnotation(
                PropertiesResource.class);
        String fileName = annotation.name();
        String loader = annotation.loader();
        Properties props = null;
        // Load the properties from file
        URL url = null;
        url = Thread.currentThread().getContextClassLoader()
                .getResource(fileName);
        if (url != null) {
            props = new Properties();
            try {
                props.load(url.openStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return props;
    }
}

Then inject it into your Named component.

@Inject
@PropertiesResource(name = "filename.properties", loader = "")
private Properties props;

I did this looking into the weld documentation where the @HttpParam is given as an example here. This is as per weld 1.1.0, in weld 1.0.0, the getting annotation can be done like this

PropertiesResource annotation = ip.getAnnotation(PropertiesResource.class);
终陌 2024-12-03 12:16:29

如果您使用的应用程序服务器将 WELD 作为 CDI 实现(例如 Glassfish 3.x 或 JBoss 7.x 或 Weblogic 12),那么您需要使用 WELD 扩展,这在 WELD 中进行了解释文档 此处

就像将其添加到 POM 中一样简单

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

If the application server you are using has WELD as the CDI implementation (Glassfish 3.x or JBoss 7.x or Weblogic 12 are examples) 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文