Spring 使用OmniFaces@Param注释时未满足的依赖关系。没有类型为java.lang.String的限定bean

Spring 使用OmniFaces@Param注释时未满足的依赖关系。没有类型为java.lang.String的限定bean,spring,jsf-2,cdi,omnifaces,joinfaces,Spring,Jsf 2,Cdi,Omnifaces,Joinfaces,我试图在不推荐的ManagedBean/ManagedProperty注释上使用CDI,并在一个非常简单的web应用程序中遇到此异常: Error creating bean with name 'navigationController': Unsatisfied dependency expressed through field 'message'; nested exception is org.springframework.beans.factory.NoSuchBeanDefini

我试图在不推荐的ManagedBean/ManagedProperty注释上使用CDI,并在一个非常简单的web应用程序中遇到此异常:

Error creating bean with name 'navigationController': Unsatisfied dependency expressed through field 'message'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.inject.Inject(), @org.omnifaces.cdi.Param(validatorAttributes=[], validatorMessage=, validators=[], converter=, pathIndex=-1, converterAttributes=[], converterClass=interface javax.faces.convert.Converter, label=, overrideGlobalBeanValidationDisabled=false, required=false, disableBeanValidation=false, name=, validatorClasses=[], converterMessage=, requiredMessage=)}
我正试图在上的OmniFaces showcase中以@Param为例

我把它放在一页上。据我所知,NavigationController bean应该在导航到nav.xhtml时创建,并且消息字段将填充来自请求参数的值

IntellliJ还抱怨@Param注释:

找不到使用@Param限定的bean

谢谢你的帮助。我不知道下一步该做什么

整个项目正在进行中

nav.xhtml的内容如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

<h:body >
<f:view>

    <p:panel id="myPanelId" header="My Panel" style="margin-bottom:20px">
        <h:outputText value="My text output." />
        <h:outputText value="My text output from params: #{navigationController.action}" />
    </p:panel>

</f:view>
</h:body>
</html>
xml是

<?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>org.nrg.cap</groupId>
    <artifactId>jsfSpringBootApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>JSF Spring Boot WebApp</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>

        <joinfaces.version>4.0.0</joinfaces.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.joinfaces</groupId>
                <artifactId>joinfaces-dependencies</artifactId>
                <version>${joinfaces.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.joinfaces</groupId>
            <artifactId>primefaces-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.joinfaces</groupId>
            <artifactId>omnifaces3-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0.SP1</version>
        </dependency>
    </dependencies>

</project>

4.0.0
org.nrg.cap
jsfSpringBootApp
1.0-快照
战争
JSF Spring Boot WebApp
UTF-8
1.8
4.0.0
org.springframework.boot
spring启动程序父级
2.1.1.1发布
org.joinfaces
joinfaces依赖项
${joinfaces.version}
聚甲醛
进口
org.joinfaces
primefaces弹簧启动机
org.joinfaces
omnifaces3弹簧靴启动器
javax.enterprise
CDIAPI
2.0.SP1
我发现我必须为CDIAPI添加依赖项,因为我要部署到Tomcat9.0.13,它没有嵌入cdi。
pom源于joinfaces项目。

您应该在bean.xml集合
bean discovery mode=“all”>

我看得更深:基于Joinfaces的项目,这意味着项目的基础是一个Spring boot。 因此,在将CDI用于DI之后。将CDI注释更改为Spring DI-@Inject到@Autowired。
连接两个不同的DI原则不是一个好主意。

OmniFaces
@Param
注释是基于CDI(实现)的注释。尽管Spring可以使用
@Named
@Inject
作为
@Component
@Autowired
的替代品,如中所述(您确实需要添加“CDI”api),但它仍然不能使Spring成为真正的CDI容器

这意味着Spring不会使用/解释
@Param
,因为OmniFaces/CDI打算使用
@Param
注释,而Spring会尝试使用
@Param
注释找到一个真正的bean,而注释当然是不存在的。因此,你会得到你得到的错误

最佳解决方案是什么,超出了这个问题的范围,因为它“主要基于意见”,但基本上可以归结为两种选择

  • 由于JSF2.4需要CDI和用户嵌入的Tomcat(或JavaEE),所以将Spring(引导)放在DI上,如中所述(不需要使用微服务!)
  • 一直使用spring,切换到SpringMVC,放弃JSF和Omnifaces

我不会在这里说第一个有我的偏好。。。嗯,我只是

@Param
是CDI注释,尽管spring能够识别
@命名的
@Inject
,但它不是CDI容器,而是spring。从Spring切换到真正的cdi很可能解决了您使用joinfaces时的问题,这意味着base是Spring boot,但您也将cdi用作DI。我认为这是反模式的,更喜欢使用Spring DI。虽然OP声明CDI用于注入,但OP不是使用CDI进行注入,而是使用Spring(请参见错误)。所以这与豆子的发现无关。而且一个
@Param
注释并不是关于一个真正的bean,甚至…错了,Spring的当前版本威胁到cdi注释与它自己的注释完全相同。更改它们仍然不能使spring理解
@Param
。实现这一点的唯一方法是不对DI使用spring,而是使用真正的cdi,或者使用“类似”的注释,而不是纯spring(我不知道,因为我没有使用Spring不同意,Spring DI不同于CDI。为了合并,项目必须按照我之前的答案重新配置,但它是反模式的,并且添加了失败层。我看到很多项目,CDI和Spring一起使用,是您无法升级和支持的项目。我们应该理解CDI和Spring行为随每个新版本的变化而变化。JSF本身也不需要与Spring合并。不,又错了。我不是说“合并”。OP使用Spring进行DI,并添加了CDI API,使Spring能够识别
@Inject
@Named
,因为注释可以看出OP就是这么做的。这在t您不能使用其他真正的CDI注释,如OmniFaces
@Param
,因此对OP的期望是错误的。切换到Spring注释不会使OmniFaces中的@Param起作用。因此OP也需要一个Spring替换
<?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>org.nrg.cap</groupId>
    <artifactId>jsfSpringBootApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>JSF Spring Boot WebApp</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>

        <joinfaces.version>4.0.0</joinfaces.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.joinfaces</groupId>
                <artifactId>joinfaces-dependencies</artifactId>
                <version>${joinfaces.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.joinfaces</groupId>
            <artifactId>primefaces-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.joinfaces</groupId>
            <artifactId>omnifaces3-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0.SP1</version>
        </dependency>
    </dependencies>

</project>