gpt4 book ai didi

java - Thorntail/Wildfly Swarm 中的 CDI 注入(inject)在 Controller 外部不起作用

转载 作者:行者123 更新时间:2023-12-02 01:55:29 24 4
gpt4 key购买 nike

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

我的 Controller :

@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;
}
}

项目默认值.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注入(inject)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.nameMyController 类中使用时会被注入(inject),但在 Configuration 类中则不会注入(inject)。

我使用的是thorntail版本2.3.0.Final-SNAPSHOT

您知道可能是什么原因造成的吗?谢谢。

最佳答案

问题出在这行代码中:

new Configuration().getName()

我应该注入(inject)配置对象,如下所示:

@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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com