简体   繁体   English

根据环境属性启用CDI装饰器

[英]Enabling CDI Decorator based on environmental properties

I know that it is possible to configure Decorators in the "beans.xml" file that is embedded in the EAR to be deployed. 我知道可以在要部署的EAR中嵌入的“ beans.xml”文件中配置Decorators。

The problem is that I use the same EAR for all the environments, and the set of properties or specific configurations are stored in some folder outside the package. 问题是我在所有环境中都使用相同的EAR,并且属性或特定配置集存储在软件包外部的某个文件夹中。

I need to determine if a Decorator will be used or not "external beans.xml" or some similar mechanism (something that is outside the EAR). 我需要确定是否使用Decorator,或者是否使用“ external beans.xml”或某种类似的机制(在EAR之外的某种东西)。

Any ideas? 有任何想法吗?

Thank you very much. 非常感谢你。

Normally, this won't work - standard means of enablement are beans.xml for per-archive approach and @Priority for global enablement. 通常,这是行不通的-启用的标准方法是针对每个归档方法的beans.xml和用于全局启用的@Priority There is nothing like "external beans.xml". 没有像“ external beans.xml”这样的东西。

Although there is a way to enable it with extension . 尽管有一种方法可以通过extension启用它。 You need to set up an extension and observe AfterTypeDiscovery event . 您需要设置扩展并观察AfterTypeDiscovery事件 From there you can make use of public List<Class<?>> getDecorators(); 从那里可以使用public List<Class<?>> getDecorators(); which returns MUTABLE list of decorators - so you can add your own into the list (in a form of a Class ). 它返回装饰器的MUTABLE列表-因此您可以将自己的装饰器添加到列表中(以Class的形式)。 That should enable it. 那应该启用它。

Another scenario you can use, is to utilize build-time inclusion and processing. 您可以使用的另一种情况是利用构建时包含和处理。 If you know before hand, what properties activate specific decorators at build-time, then you can use maven resources, together with system properties, to define additional resources to be filtered, thus: 如果您事先知道,哪些属性会在构建时激活特定的装饰器,那么您可以使用Maven资源以及系统属性来定义要过滤的其他资源,因此:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompant</groupId>
    <artifactId>my-project-id</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <some.kind.of.selector.properties>$basedir}/src/main/resources/development</some.kind.of.selector.properties>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>${some.kind.of.selector.properties}</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <some.kind.of.selector.properties>$basedir}/src/main/resources/test</some.kind.of.selector.properties>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <some.kind.of.selector.properties>$basedir}/src/main/resources/prod</some.kind.of.selector.properties>
            </properties>
        </profile>
    </profiles>
</project>

The at build time, you can specify different beans.xml for every environment: 在构建时,可以为每个环境指定不同的beans.xml

mvn clean install -Pprod mvn全新安装-Pprod

or even specify the property directly 甚至直接指定属性

mvn clean install -Dsome.kind.of.selector.properties=/path/to/additional/resources mvn全新安装-Dsome.kind.of.selector.properties = /路径/到/其他/资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM