我正在尝试使用应从@ConfigurationValue读取值的project-defaults.yml,但是,我遇到了奇怪的行为,请参见下面的代码。

MyController:

@ApplicationScoped
@Path("/app")
public class MyController {
    @Inject
    @ConfigurationValue("database.name")
    private String name;

    @GET
    @Path("/name-1")
    @Produces(MediaType.TEXT_PLAIN)
    public String name1() {
        return String.valueOf(name); // displays postgres
    }

    @GET
    @Path("/name-2")
    @Produces(MediaType.TEXT_PLAIN)
    public String name2() {
        return String.valueOf(new Configuration().getName()); // displays null
    }
}

@ApplicationScoped
class Configuration {
    @Inject
    @ConfigurationValue("database.name")
    private String name;

    public String getName() {
        return name;
    }
}


project-defaults.yml

database:
  name: postgres


父pom.xml :(与https://github.com/thorntail/thorntail-examples/blob/master/pom.xml大致相同)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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.example.test</groupId>
    <artifactId>parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <name>Parent</name>
    <description>Parent</description>

    <packaging>pom</packaging>

    <properties>
        <version.thorntail>2.3.0.Final-SNAPSHOT</version.thorntail>
        <maven.min.version>3.2.1</maven.min.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <repositories>
        <repository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>jboss-public-repository-group</id>
            <name>JBoss Public Repository Group</name>
            <url>http://repository.jboss.org/nexus/content/groups/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>sonatype-nexus-snapshots</id>
            <name>Sonatype Nexus Snapshots</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring</id>
            <name>Spring releases</name>
            <url>http://repo.spring.io/libs-release-remote/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <finalName>${project.artifactId}</finalName>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>io.thorntail</groupId>
                    <artifactId>thorntail-maven-plugin</artifactId>
                    <version>${version.thorntail}</version>
                    <configuration>
                        <jvmArguments>
                            <jvmArgument>-Xmx128m</jvmArgument>
                        </jvmArguments>
                    </configuration>
                    <executions>
                        <execution>
                            <id>package</id>
                            <goals>
                                <goal>package</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.19.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <systemPropertyVariables>
                            <org.apache.maven.user-settings>${session.request.userSettingsFile.path}
                            </org.apache.maven.user-settings>
                        </systemPropertyVariables>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>build-helper-maven-plugin</artifactId>
                    <version>1.10</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/it/java</source>
                            </sources>
                        </configuration>
                    </execution>
                    <execution>
                        <id>add-resource</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-test-resource</goal>
                        </goals>
                        <configuration>
                            <resources>
                                <resource>
                                    <directory>src/it/resources</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                    <systemPropertyVariables>
                        <phantomjs.binary.version>2.1.1</phantomjs.binary.version>
                        <org.apache.maven.user-settings>${session.request.userSettingsFile.path}
                        </org.apache.maven.user-settings>
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>arquillian</artifactId>
            <version>${version.thorntail}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.graphene</groupId>
            <artifactId>graphene-webdriver</artifactId>
            <version>2.1.0.Alpha2</version>
            <type>pom</type>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.thorntail</groupId>
                <artifactId>bom-all</artifactId>
                <version>${version.thorntail}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian</groupId>
                <artifactId>arquillian-bom</artifactId>
                <version>1.1.12.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.jboss.arquillian.extension</groupId>
                <artifactId>arquillian-drone-bom</artifactId>
                <version>2.0.1.Final</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>cdi-injection</module>
    </modules>

    <profiles>
        <profile>
            <id>docker</id>
        </profile>
        <profile>
            <id>uberjar</id>
            <properties>
                <thorntail.useUberJar>true</thorntail.useUberJar>
            </properties>
        </profile>
        <profile>
            <id>not-skipping-tests</id>
            <activation>
                <property>
                    <name>!skipTests</name>
                </property>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>io.thorntail</groupId>
                            <artifactId>thorntail-maven-plugin</artifactId>
                            <version>${version.thorntail}</version>
                            <executions>
                                <execution>
                                    <id>start</id>
                                    <phase>pre-integration-test</phase>
                                    <goals>
                                        <goal>start</goal>
                                    </goals>
                                    <configuration>
                                        <stdoutFile>target/stdout.log</stdoutFile>
                                        <stderrFile>target/stderr.log</stderrFile>
                                    </configuration>
                                </execution>
                                <execution>
                                    <id>stop</id>
                                    <phase>post-integration-test</phase>
                                    <goals>
                                        <goal>stop</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
</project>


cdi-injection pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>

    <parent>
        <groupId>com.example.test</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <artifactId>cdi-injection</artifactId>

    <name>CDI Injection</name>
    <description>CDI Injection</description>

    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>io.thorntail</groupId>
                <artifactId>thorntail-maven-plugin</artifactId>
                <version>${version.thorntail}</version>
                <configuration>
                    <bundleDependencies>true</bundleDependencies>
                </configuration>
                <executions>
                    <execution>
                        <id>package</id>
                    </execution>
                    <execution>
                        <id>start</id>
                    </execution>
                    <execution>
                        <id>stop</id>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>monitor</artifactId>
            <version>${version.thorntail}</version>
        </dependency>
        <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>jaxrs</artifactId>
            <version>${version.thorntail}</version>
        </dependency>
        <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>cdi</artifactId>
            <version>${version.thorntail}</version>
        </dependency>
        <dependency>
            <groupId>io.thorntail</groupId>
            <artifactId>logging</artifactId>
            <version>${version.thorntail}</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.4</version>
        </dependency>
    </dependencies>
</project>


database.name类中使用时,将注入值MyController,但在Configuration类中使用时,则不会注入。

我正在使用thorntail版本2.3.0.Final-SNAPSHOT

您有什么想法会导致它吗?谢谢。

最佳答案

问题出在这行代码中:

new Configuration().getName()


我应该改为注入Configuration对象,如下所示:

@ApplicationScoped
@Path("/app")
public class MyController {
    @Inject
    @ConfigurationValue("database.name")
    private String name;

    @Inject
    private Configuration configuration;
}

关于java - Thorntail/Wildfly Swarm中的CDI注入(inject)在 Controller 外部不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52375436/

10-10 17:26